Question

I just asked a similar question but (sorry!) I think I'll need more help. I have a problem with signals in pyqt. Let me post the whole code, it isn't long and it is easier for me to explain...

from PyQt4 import QtGui, QtCore, Qt
import time
import math

class FenixGui(QtGui.QWidget):

    def backgroundmousepressevent(self, event):
        print "test 1"
        self.offset = event.pos()


    def backgroundmousemoveevent(self, event):
        print "test 2"
        x=event.globalX()
        y=event.globalY()
        x_w = self.offset.x()
        y_w = self.offset.y()
        self.move(x-x_w, y-y_w)


    def __init__(self):
        super(FenixGui, self).__init__()

        # setting layout type
        hboxlayout = QtGui.QHBoxLayout(self)
        self.setLayout(hboxlayout)

        # hiding title bar
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        # setting window size and position
        self.setGeometry(200, 200, 862, 560)
        self.setAttribute(Qt.Qt.WA_TranslucentBackground)
        self.setAutoFillBackground(False)

        # creating background window label
        backgroundpixmap = QtGui.QPixmap("fenixbackground.png")
        self.background = QtGui.QLabel(self)
        self.background.setPixmap(backgroundpixmap)
        self.background.setGeometry(0, 0, 862, 560)

        # making window draggable by the window label
        self.connect(self.background,QtCore.SIGNAL("mousePressEvent()"),         self.backgroundmousepressevent)
        self.connect(self.background,QtCore.SIGNAL("mouseMoveEvent()"), self.backgroundmousemoveevent)


        # fenix logo
        logopixmap = QtGui.QPixmap("fenixlogo.png")
        self.logo = QtGui.QLabel(self)
        self.logo.setPixmap(logopixmap)
        self.logo.setGeometry(100, 100, 400, 150)


def main():

    app = QtGui.QApplication([])
    exm = FenixGui()
    exm.show()
    app.exec_()


if __name__ == '__main__':
    main()

All right, so this is the code, it's just a simple gui that I wanted to make draggable around the screen clicking and dragging on any place in the background. My problem is: backgroundmousepressevent and backgroundmousemoveevent DON'T get fired when i press or move the button. So I wonder: where's the error? Did I mispell something or what? Thank you very much!

Matteo

Was it helpful?

Solution

In Qt, events are different from signals and slots. Events are represented by QEvent objects that are passed to the event() method of QObjects, where they are typically dispatched to specialized methods such as mousePressEvent and mouseMoveEvent. Since they are not signals, you cannot connect them to slots.

Instead, just re-implement event functions to do custom things. Make sure to call the original implementation with super, though, unless you know what you are doing.

def mousePressEvent(self, event):
    super(FenixGui, self).mousePressEvent(event)
    print "test 1"
    self.offset = event.pos()

def mouseMoveEvent(self, event):
    super(FenixGui, self).mouseMoveEvent(event)
    print "test 2"
    x=event.globalX()
    y=event.globalY()
    x_w = self.offset.x()
    y_w = self.offset.y()
    self.move(x-x_w, y-y_w)

Typically, Qt will warn you when trying to connect to non-existing signals, by writing a warning message to the console. Additionally, you can prevent this situation by using new-style signals and slots instead of the old-style, more C++-ish SIGNAL() function:

lineEdit = QtGui.QLineEdit()
lineEdit.valueChanged.connect(self.myHandlerMethod)

OTHER TIPS

You're trying to connect to QWidget's mousePressEvent and mouseMoveEvent signals, but they don't exist as signals. Try overriding the methods instead. This works for me:

from PyQt4 import QtGui, QtCore, Qt
import time
import math

class FenixGui(QtGui.QWidget):

    def mousePressEvent(self, event):
        print "test 1"
        self.offset = event.pos()
        QtGui.QWidget.mousePressEvent(self, event)


    def mouseMoveEvent(self, event):
        print "test 2"
        x=event.globalX()
        y=event.globalY()
        x_w = self.offset.x()
        y_w = self.offset.y()
        self.move(x-x_w, y-y_w)
        QtGui.QWidget.mousePressEvent(self, event)

    def __init__(self):
        super(FenixGui, self).__init__()

        # setting layout type
        hboxlayout = QtGui.QHBoxLayout(self)
        self.setLayout(hboxlayout)

        # hiding title bar
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        # setting window size and position
        self.setGeometry(200, 200, 862, 560)
        self.setAttribute(Qt.Qt.WA_TranslucentBackground)
        self.setAutoFillBackground(False)

        # creating background window label
        backgroundpixmap = QtGui.QPixmap("fenixbackground.png")
        self.background = QtGui.QLabel(self)
        self.background.setPixmap(backgroundpixmap)
        self.background.setGeometry(0, 0, 862, 560)

        # fenix logo
        logopixmap = QtGui.QPixmap("fenixlogo.png")
        self.logo = QtGui.QLabel(self)
        self.logo.setPixmap(logopixmap)
        self.logo.setGeometry(100, 100, 400, 150)

def main():
    app = QtGui.QApplication([])
    exm = FenixGui()
    exm.show()
    app.exec_()

if __name__ == '__main__':
    main()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top