2011-08-22 4 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

제발, 기존의 코딩 스타일을 적응하십시오. 나는 "파이썬 코딩 스타일"을 제안 할 것이다. 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 개체로 표현되며 QObjects입니다. 일반적으로 mousePressEventmouseMoveEvent과 같은 특수 메서드로 전달됩니다. 신호가 아니므로 슬롯에 연결할 수 없습니다.

대신 사용자 정의 작업을 수행하기 위해 이벤트 기능을 다시 구현하십시오. 그래도 수행중인 작업을 모르는 경우 원래 구현을 super으로 호출해야합니다.

일반적으로 Qt는 콘솔에 경고 메시지를 작성하여 존재하지 않는 신호에 연결하려고 할 때 경고합니다. 당신은는 QWidget의 mousePressEvent 및 mouseMoveEvent 신호에 연결하려는

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

대단히 감사합니다. –

1

,하지만 그들은 존재하지 않는 : 틱 SIGNAL() 기능 - 또한 ++ 대신 이전 스타일의 더 C를 new-style signals and slots를 사용하여 이러한 상황을 방지 할 수 있습니다 신호로. 대신 메서드를 재정의하십시오. 이 작품은 나를 위해 :

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()