3

Questions and Briefing

Hello. I am trying to fix a problem with my PyQt5 window which has a custom Title Bar. If you maximize the window and you hover over the title bar with the cursor and drag the window to move it, the window will resize and you will be able to move it as normal.

However, when the window restores previous state before it is maximized using the cursor click and drag method, the cursor does not grip onto the location on the title bar to move the window, but, it stays in the same place where the click and drag method was started so the cursor is on the other side of the screen mysteriously moving the window.

  • How would I make it so the cursor would grip onto the title bar and when the window is restored using the click and drag method it would be locked on to that same location, the same as you would have for normal applications.

Sub-Questions

  • How would I get the location of the cursor inside the window? I have managed to get the cursor location using QCursor.pos() but that returns the global position on the actual screen but I think I need it on the window so I can try to make it so the cursor grips onto the title bar.

I have tried to...

  • Use QCursor.pos() before the click and drag restoration method is done and then I did QCursor.setPos() to set the previously captured position, it does work but then it causes the window to move with it and the cursor is also out of position from the intended grip location inside the window.

Code

class MainWindow(QMainWindow): # Main Window
    def __init__(self): # Main initialisation
        super(MainWindow, self).__init__() # Call superclass

        # UI Setup
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self) 

        # Move Window
        def move_window(event):
            # Restore Window (before movement)
            if UIFunctions.return_status(self) == 1: # If maximized
                UIFunctions.maximize_restore(self)
                # QCursor.setPos(self.previous_cursor_pos)

            if event.buttons() == Qt.LeftButton: # If Left Click (move window)
                self.move(self.pos() + event.globalPos() - self.dragPos)
                self.dragPos = event.globalPos()
                event.accept()

        # Set Title Bar
        self.ui.title_bar.mouseMoveEvent = move_window

        # Set UI Definitions
        UIFunctions.ui_definitions(self)

        self.show() # Show main window

    def mousePressEvent(self, event): # Mouse Press Event
        self.dragPos = event.globalPos()

(Main Class with Move Window method)

Other Info

Python Version: Python 3.8.6
Module Used: PySide2

Happy to provide more information.

Kevin A.
  • 99
  • 2
  • 2
  • 10
  • 1
    Without any code reference it's almost impossible to answer you (also, avoid extended formatting, it's just distracting and doesn't actually bring more focus to what you write). Please, provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Btw: to get the cursor position in *widget* coordinates, just use [mapFromGlobal()](https://doc.qt.io/qt-5/qwidget.html#mapFromGlobal): for example, to map to "self", use `self.mapFromGlobal(QCursor.pos())`. – musicamante Dec 06 '20 at 00:17
  • How would I move the cursor to the widget and that location? – Kevin A. Dec 08 '20 at 20:25
  • Your code is **not** reproducible. Where is the code of the ui? What is UIFunctions? Please, follow the indications in the "minimal reproducible example" link given above. We **must** be able to copy and paste your code possibly without any other modifications. – musicamante Dec 08 '20 at 21:31

1 Answers1

0

musicmante's solution in the comments fixed my issue. I was trying to use something like self.pos.x() and after I switched it to self.mapFromGlobal(QCursor.pos()).x()

Bill
  • 698
  • 1
  • 5
  • 22
Muzaffer D
  • 43
  • 7