python - mousepress事件的问题

标签 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 不会被触发。所以我想知道:错误在哪里?我是否拼写错误或什么?非常感谢!

马泰奥

最佳答案

在 Qt 中,事件不同于信号和槽。事件由 QEvent 对象表示,这些对象被传递到 QObjectevent() 方法,在该方法中事件通常被分派(dispatch)给专门的方法,例如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++ 的 SIGNAL() 函数:

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

关于python - mousepress事件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7144705/

相关文章:

python - 有没有一种方法可以使用 Kivy 和 Python 在 TextInput 字段中显示数字键盘(最好带有气泡)?代码的新问题

javascript - knockout 使用 mouseenter 事件绑定(bind)而不是 mouseover

鼠标移动时忽略 Java 鼠标事件?

python - 如何获取外键 django 引用的特定 id 的数据?

Python 字典键缺失

javascript - 在 python 服务器上从 Fabric.js JSON 构造图像

python - 在 PySide2 中设置垂直和水平对齐方式

python - SVG 加载到框架

python - PyQt:仅适用于整数或仅字符串和长度限制的 QLineEdit 输入掩码

javascript - 使用 JavaScript 检测页面中的任何鼠标事件