Draggable window with pyqt4

前端 未结 1 812
情书的邮戳
情书的邮戳 2021-02-11 00:40

just a simple question: I\'m using pyqt4 to render a simple window. Here\'s the code, I post the whole thing so it easier to explain.

from PyQt4 import QtGui, Qt         


        
相关标签:
1条回答
  • 2021-02-11 01:14

    You can override mousePressEvent() and mouseMoveEvent() to get the location of the mouse cursor and move your widget to that location. mousePressEvent will give you the offset from the cursor position to the top left corner of your widget, and then you can calculate what the new position of the top left corner should be. You can add these methods to your FenixGui class.

    def mousePressEvent(self, event):
        self.offset = event.pos()
    
    def mouseMoveEvent(self, event):
        x=event.globalX()
        y=event.globalY()
        x_w = self.offset.x()
        y_w = self.offset.y()
        self.move(x-x_w, y-y_w)
    
    0 讨论(0)
提交回复
热议问题