views:

136

answers:

1

Hi,

I've got a Widget (QTabeleWidget, QLabels and some QButtons). It was build in Qt-Designer. And now I have to implement some things. For that I need the mousePressEvent. Usually I would write a subclass and write something like this:

    def mousePressEvent(self, event):
    if event.button() == Qt.LeftButton:
        print "left"
    else:
        print 'right'

But I don't know how to do that for a Widget created in the Designer. I need it for the QTabeleWidget. Hope someone can help me. I tried to solve the problem with the help of google, but without success. This site helped me many times, so I thought I'll give it a shot and ask.

A: 

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:

class MyTableWidget(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.ui
    def mousePressEvent(self, event):
        pass # do something useful

Multiple Inheritance:

class MyTableWidget(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 form
    def mousePressEvent(self, event):
        pass # do something useful

Dynamically Generated:

from PyQt4 import uic
yourFormTypeInstance = 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.

Kaleb Pederson
Thank you for the answer. I tried it, but it doesn't work. If I click on the window, not a widget, then it triggers. The mousePressEvent works. But if I'm clicking on the QTableWidget or a QLabel the mousePressEvent is not getting executed. For example, if I want the itemSelectionChanged signal from the QTableWidget, then I write: "def on_myTable_itemSelectionChanged(self):"So I'm looking for something equivalent for the mousePressEvent.Hope my explanation is not to bad.
Auto-wiring exists for signals but not for events. When you subclass and implement `mousePressEvent`, you'll receive events for the given widgets, but only for events where the child doesn't accept the event. Since you have widgets within the widget, each of them receives and processes the event before the parent sees it. One good way to deal with that when no relevant signals are present is to install an event filter using `installEventFilter`.
Kaleb Pederson
Thank you! It works with installEventFilter. That's what I was looking for.