我剛剛問了一個類似的問題,但是(對不起!)我想我需要更多的幫助。 pyqt中的信號有問題。讓我張貼整個代碼,它不長,它是更容易爲我解釋...mousepressevent的問題
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當我按下或移動按鈕時不會被解僱。所以我想知道:錯誤在哪裏?我拼錯了什麼或什麼?非常感謝你!
Matteo
請,請適應現有的編碼風格。我建議使用「Python編碼風格」:http://www.python.org/dev/peps/pep-0008/ 但是,由於您似乎很多使用PyQt,也許Qt編碼風格更適合你:http://developer.qt.nokia.com/wiki/Qt_Coding_Style – Constantinius
這似乎很明顯,他主要只是有複製/粘貼的問題,不是? – Profane