Rawres
Rawres

Reputation: 55

Move QtWidgets.QtWidget using mouse

I want to move a QtWidgets.QtWidget using the mouse (not a QPushButton, QLabel etc.). I've searched everywhere on the web, but couldn't find an answer for this. mousePressEvent seemed to be the way, but it doesn't work.

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_hGUI(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)

    def setupUi(self, hGUI):
        hGUI.setObjectName("hGUI")
        hGUI.resize(161, 172)
        hGUI.setMinimumSize(QtCore.QSize(200, 200))
        hGUI.setMaximumSize(QtCore.QSize(200, 200))

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    hGUI = QtWidgets.QWidget()
    ui = Ui_hGUI()
    ui.setupUi(hGUI)

    hGUI.show()
    sys.exit(app.exec_())

I'm using Python 3.5, I'm creating the GUI using Qt Designer, then translate it to python code.

Edit: I'm trying to move a borderless windows by click on it.

Upvotes: 1

Views: 2519

Answers (2)

yurisnm
yurisnm

Reputation: 1640

That's a really simple question sir,

Let's say you just have to have an variable that holds the position of your widget and interact with it according to your needs.

  1. This position variable let's call it "oldPos".
  2. Now inside your mouse press you update this position.
  3. By the last but not least, you relate your "oldPos" and your mouseMove actual position and move your widget.
  4. Wallahhhh, here we have a beautiful and simple movable widget by mouse events.

Here is the simplest example.

from PyQt5.QtWidgets import QWidget

class MyMovableWidget(QWidget):
    """WToolBar is a personalized toolbar."""

    homeAction = None

    oldPos = QPoint()

    def __init__(self):
        super().__init__()

     def mousePressEvent(self, evt):
        """Select the toolbar."""
        self.oldPos = evt.globalPos()

    def mouseMoveEvent(self, evt):
        """Move the toolbar with mouse iteration."""

        delta = QPoint(evt.globalPos() - self.oldPos)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = evt.globalPos()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    coolWidget = MyMovableWidget()
    coolWidget.show()
    sys.exit(app.exec_())

So simple isn't it? :D

Upvotes: 5

Rawres
Rawres

Reputation: 55

I managed to make it work, thanks to @bnaecker for telling me that the code actually creates two widgets, I've replaced some stuff in my code. Basically, just edit the code generated when you translate the .ui to .py so it would only create one widget. The most changes happened here:

if __name__ == "__main__":
    import sys
    sys.excepthook = excepthook
    app = QtWidgets.QApplication(sys.argv)
    hGUI = QtWidgets.QWidget(flags=QtCore.Qt.FramelessWindowHint)
    ui = Ui_hGUI()
    ui.setupUi(hGUI)
    hGUI.show()
    sys.exit(app.exec_())

Edited to this:

if __name__ == "__main__":
    sys.excepthook = excepthook
    app = QtWidgets.QApplication(sys.argv)
    hGUI = Ui_hGUI()
    sys.exit(app.exec_())

Add self.show() at the end of retranslateUi(self), replace every "hGUI" in the code with "self" or delete it if it's an argument (except for controls like buttons and labels). Here are both codes, non-working one vs. working one: https://gist.github.com/anonymous/0707b4fef11ae4b31cf56dc78dd3af80 Note: In the new code, the app is called "VirtualMemories".

Upvotes: -1

Related Questions