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 didQCursor.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.