2011-08-22 25 views
2

我剛剛問了一個類似的問題,但是(對不起!)我想我需要更多的幫助。 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

+0

請,請適應現有的編碼風格。我建議使用「Python編碼風格」:http://www.python.org/dev/peps/pep-0008/ 但是,由於您似乎很多使用PyQt,也許Qt編碼風格更適合你:http://developer.qt.nokia.com/wiki/Qt_Coding_Style – Constantinius

+0

這似乎很明顯,他主要只是有複製/粘貼的問題,不是? – Profane

回答

5

在Qt中,事件與信號和插槽不同。事件表示爲QEvent傳遞給event()方法QObject s的對象,其中它們通常被分派到專門的方法,如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) 
+0

非常感謝,這有幫助! –

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() 
相關問題