Draggable window with pyqt4

前端 未结 1 1309
生来不讨喜
生来不讨喜 2021-02-11 00:14

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 00:58

    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)
提交回复
热议问题