PDA

View Full Version : QComboBox not emitting signal when it's editable and using a model



fede
30th March 2014, 21:48
I am trying to catch the highlighted signal from a QComboBox, but it is not being emitted consistently. So far I've found out that:
1. If QComboBox is not editable, the highlighted signal is emitted correctly.
2. If QComboBox is editable and uses the default convenience model (ie no call to setModel is made), the highlighted signal is emitted.
BUT:
3. If QComboBox is editable and it uses a model other than the default convenience model, the highlighted signal is not emitted.
I've tried this using both a standard QStringList model and a custom model.

Is there a reason for this behavior? Am I missing something?
A code example follows, where combos 1-3 illustrate the 3 cases outlined above, and combo3 is not emitting the highlighted signal while combos 1 and 2 are.

Thanks!


import sys
from PyQt4 import QtCore
from PyQt4 import QtGui

class TestWindow(QtGui.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
data_items = ['item1', 'item2', 'item3', 'item4']
combo1 = QtGui.QComboBox(self)
combo1.setEditable(False)
combo1.setModel(QtGui.QStringListModel(data_items, self))
combo1.highlighted.connect(lambda i: messages.append('combo1 emits highlighted signal, item={}'.format(i)))
combo2 = QtGui.QComboBox(self)
combo2.setEditable(True)
combo2.addItems(data_items)
combo2.highlighted.connect(lambda i: messages.append('combo2 emits highlighted signal, item={}'.format(i)))
combo3 = QtGui.QComboBox(self)
combo3.setEditable(True)
combo3.setModel(QtGui.QStringListModel(data_items, self))
combo3.highlighted.connect(lambda i: messages.append('combo3 emits highlighted signal, item={}'.format(i)))
messages = QtGui.QTextEdit(self)
layout = QtGui.QVBoxLayout()
layout.addWidget(combo1)
layout.addWidget(combo2)
layout.addWidget(combo3)
layout.addWidget(messages)
self.setLayout(layout)


if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = TestWindow()
window.show()
app.exec_()