views:

38

answers:

1

Hi

I'm having a problem with QComboBox not allowing me to change the edit text to anything existing item of differing case.

Example code is below. What I'd like to do is enter 'one' into a combo box already containing the item 'One' without the side effect of the text being changed to 'One'. Currently it's changed back to 'One' as soon as the combo box loses focus.

Disabling AutoCompletionCaseSensitivity works, but it has the side effect of not being useful (Doesn't eg. show completions for 'one').

I've also tried overriding the focusOutEvent of QComboBox and restoring the correct text, but then things like copy-paste don't work. Changing the completer hasn't helped any either.

The fact combo boxes behave this way is detrimental to my app. If anyone has any ideas (or I missed something obvious), please let me know.

I'm using Qt 4.6.2 and PyQt 4.7.2 on Ubuntu 10.04, but have experienced this on other distros/Qt versions above 4.5.

Thanks and Regards

Example Code:

from PyQt4.QtGui import * 
from PyQt4.QtCore import SIGNAL, Qt 

class Widget(QWidget): 
    def __init__(self, parent=None): 
        super(Widget, self).__init__(parent) 
        combo = QComboBox() 
        combo.setEditable(True) 
        combo.addItems(['One', 'Two', 'Three'])
        lineedit = QLineEdit() 

        layout = QVBoxLayout() 
        layout.addWidget(combo) 
        layout.addWidget(lineedit) 
        self.setLayout(layout) 

app = QApplication([]) 
widget = Widget() 
widget.show() 
app.exec_()
A: 
from PyQt4.QtGui import * 
from PyQt4.QtCore import SIGNAL, Qt, QEvent


class MyComboBox(QComboBox):
    def __init__(self):
        QComboBox.__init__(self)

    def event(self, event):
        if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Return:
            self.addItem(self.currentText())

        return QComboBox.event(self, event)

class Widget(QWidget): 
    def __init__(self, parent=None): 
        super(Widget, self).__init__(parent) 
        combo = MyComboBox() 
        combo.setEditable(True) 
        combo.addItems(['One', 'Two', 'Three'])
        lineedit = QLineEdit() 

        layout = QVBoxLayout() 
        layout.addWidget(combo) 
        layout.addWidget(lineedit) 
        self.setLayout(layout) 

app = QApplication([]) 
widget = Widget() 
widget.show() 
app.exec_()

The only issue with this is that it will allow adding duplicates to your combobox. I tried adding a self.findText(...) to the if statement but even Qt.MatchExactly | Qt.MatchCaseSensitive would match "bla", "bLa" and "BLA". I guess you'll find out.

something
Thanks, but it doesn't help much. It doesn't cater for the user pressing Tab or clicking on any other widget.
concentricpuddle