我刚刚问了一个类似的问题,但是(对不起!)我想我需要更多帮助。我对 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 不会被触发。所以我想知道:错误在哪里?我拼错了什么还是什么?非常感谢你!
马泰奥