[PySide] QComboBox + focusOutEvent == double focus??

anatoly techtonik techtonik at gmail.com
Wed May 23 20:27:11 CEST 2012


I am trying to make an editable ComboBox that saves its value when
focus is lost (for example, when user clicks a button on the same
page). I've defined focusOutEvent() for it, it seems to be called ok,
but focus marker never leaves the widget. It fact it duplicates. Any
hints what's going on that how to fix it?


from PySide.QtGui import *
from PySide.QtCore import 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)

    def focusOutEvent(self, event):
        self.addItem(self.currentText())
        return 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_()

--
anatoly t.



More information about the PySide mailing list