mousepressevent的问题

时间:2011-08-22 08:17:13

标签: python pyqt mouseevent signals mousepress

我刚问了一个类似的问题但是(对不起!)我想我需要更多的帮助。 pyqt中的信号有问题。让我发布整个代码,时间不长,我更容易解释......

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()

好吧,这就是代码,它只是一个简单的gui,我想在屏幕上点击并拖动背景中的任何地方进行拖动。我的问题是:backgroundmousepressevent和backgroundmousemoveevent当我按下或移动按钮时不要被解雇。所以我想知道:错误在哪里?我错了什么或什么?非常感谢你!

利玛

2 个答案:

答案 0 :(得分:5)

在Qt中,事件与信号和插槽不同。事件由传递给QEvent event()方法的QObject对象表示,通常会将它们分派到mousePressEventmouseMoveEvent等专门方法。由于它们不是信号,因此无法将它们连接到插槽。

相反,只需重新实现事件函数来执行自定义操作。不过,请务必使用super调用原始实现,除非您知道自己在做什么。

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)

通常,Qt会在尝试连接到不存在的信号时通过向控制台写入警告消息来警告您。此外,您可以使用new-style signals and slots而不是旧式,更多C ++ - ish SIGNAL()函数来防止出现这种情况:

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

答案 1 :(得分:1)

您正在尝试连接到QWidget的mousePressEvent和mouseMoveEvent信号,但它们不作为信号存在。尝试重写方法。这对我有用:

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()