Skip to content Skip to sidebar Skip to footer

How To Implement Mousepressevent For A Qt-designer Widget In Pyqt

I've got a Widget (QTabeleWidget, QLabels and some QButtons). It was built in Qt-Designer, and now I have to implement some things. For that I need the mousePressEvent. Usually I w

Solution 1:

With PyQt there are three different ways to work with forms created in designer:

  1. Use single inheritance and make the form a member variable
  2. Use multiple inheritance
  3. Dynamically generate the members directly from the UI file

Single Inheritance:

classMyTableWidget(QTableWidget):def__init__(self, parent, *args):
        super(MyTableWidget, self).__init__(parent, args)
        self.ui = YourFormName()
        self.ui.setupUi(self)
        # all gui elements are now accessed through self.uidefmousePressEvent(self, event):
        pass # do something useful

Multiple Inheritance:

classMyTableWidget(QTableWidget, YourFormName):def__init__(self, parent, *args):
        super(MyTableWidget, self).__init__(parent, args)
        self.setupUi(self)
        # self now has all members you defined in the formdefmousePressEvent(self, event):
        pass # do something useful

Dynamically Generated:

from PyQt4 importuicyourFormTypeInstance= uic.loadUi('/path/to/your/file.ui')

For (3) above, you'll end up with an instance of whatever base type you specified for your form. You can then override your mousePressEvent as desired.

I'd recommend you take a look at section 13.1 in the PyQt4 reference manual. Section 13.2 talks about the uic module.

Post a Comment for "How To Implement Mousepressevent For A Qt-designer Widget In Pyqt"