鼠标按键事件的问题
我刚才问了一个类似的问题,但(抱歉!)我觉得我还需要更多的帮助。我在使用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()
好的,这就是代码,它只是一个简单的图形界面,我想让它可以在屏幕上拖动,只要点击并拖动背景的任何地方就可以了。我的问题是:当我按下或移动按钮时,backgroundmousepressevent和backgroundmousemoveevent并没有被触发。所以我在想:错误出在哪里?我是不是拼错了什么,还是别的什么问题?非常感谢!
Matteo
2 个回答
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()
5
在Qt中,事件和信号、槽是不同的东西。事件是用QEvent
对象来表示的,这些对象会被传递给QObject
的event()
方法,然后通常会被分发到一些专门的方法,比如mousePressEvent
和mouseMoveEvent
。因为事件不是信号,所以你不能把它们连接到槽。
相反,你可以重新实现事件函数来做一些自定义的事情。不过,要记得用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会在控制台上给你发出警告信息。此外,你可以通过使用新式信号和槽来避免这种情况,而不是使用旧式的、更像C++的SIGNAL()
函数:
lineEdit = QtGui.QLineEdit()
lineEdit.valueChanged.connect(self.myHandlerMethod)