[PyQt] Moving a window without a title bar (was Fwd: Help PyQt5)

David Boddie david at boddie.org.uk
Sun Aug 14 15:34:59 BST 2016


On Sun, 14 Aug 2016 02:52:35 +0000, Sabroso Rico wrote:

> Hello as I can make my window moves with the right mouse click (given that
> the window has no title bar)
> 
> I have this function but not put to antema thank you very much
> 
> def move(self):
> pass

One way to do this is to handle the window's mouse events by implementing
the mousePressEvent and mouseMoveEvent handler methods in a widget subclass:

class Window(QWidget):

In the first method, you would do something like this, checking that the right
mouse button is pressed and store the screen position of the mouse cursor:

    def mousePressEvent(self, event):
    
        if event.buttons() == Qt.RightButton:
            self.dragPos = event.globalPos()
            event.accept()

In the second, you would check that the right button is pressed and move the
window by the change in the position of the mouse cursor, storing the new
position for the next time the event handler is called:

    def mouseMoveEvent(self, event):
    
        if event.buttons() == Qt.RightButton:
            self.move(self.pos() + event.globalPos() - self.dragPos)
            self.dragPos = event.globalPos()
            event.accept()

Note that this only handles right clicks on the window itself. Any clicks on
child widgets within the window will be handled by those widgets instead.

David
-------------- next part --------------
A non-text attachment was scrubbed...
Name: move.py
Type: text/x-python
Size: 559 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160814/1e53ffdf/attachment.py>


More information about the PyQt mailing list