1

this example

helped me a lot in understanding of how does the events work.

But I have another problem. After an event when I want to call a function of a main class it seems like it was starting from Filter class and, unfortunately I'm not able to fetch the content from Designer-made file.

class Filter(QtCore.QObject):
    def eventFilter(self, widget, event):
        if event.type() == QtCore.QEvent.FocusOut:
            print 'focus out'
            print widget.objectName()
            if widget.objectName() == 'edit_notes':
                StartQT4().object_edit_notes('edit_notes')
            return False
        else:
            return False

class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self._filter = Filter()
        self.ui.edit_notes.installEventFilter(self._filter)

    def object_edit_notes(self, w):

        self.__init__()
        something = self.ui.edit_notes.toPlainText()
        something = unicode(something).encode('utf-8')
        print something
        return False

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

Attribute .something prints nothing. I tried to call identical function with the signal method button clicked() and it works fine.

Can you help me with this?

Community
  • 1
  • 1
Maciek
  • 183
  • 1
  • 8

1 Answers1

0

You don't need a separate class for the event-filter. Any object which inherits QObject or QWidget will do, which includes QMainWindow.

So move the event-filter into your StartQT4 class, like this:

class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        # filter the events through self
        self.ui.edit_notes.installEventFilter(self)

    def object_edit_notes(self, w):
        # this will convert a QString to a python unicode object
        something = unicode(self.ui.edit_notes.toPlainText())
        print something

    def eventFilter(self, widget, event):
        if (event.type() == QtCore.QEvent.FocusOut and
            widget is self.ui.edit_notes):
            print 'focus out'
            self.object_edit_notes('edit_notes')
            return False
        return QMainWindow.eventFilter(self, widget, event)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336