Python Forum
[PyQt] Drag and Move window from menubar
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Drag and Move window from menubar
#1
Hello,

I got rid of Os windows frame because we cannot change colors. (I found clues on how to do it with C++ but can't find python's code).

Problem is we need to find a way to resize and move the windows, which could be easily found on the internet.

But I am stuck with the menubar, I can't move/resize anything from it. It seems like it is not considered as a part of the main windows. When the mouse is over it the mouse-event doesn't work.

Any idea how I could make it ? Or any link for homemade menubar-like ?

Here below is the code so far.

X=0
X2=8  
Y=0
Y2=30

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'TEST'
        self.initUI()
        
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.resize(640, 480)
        self.widget = QWidget()
        self.setCentralWidget(self.widget)
        self.setMouseTracking(True)
        self.widget.setMouseTracking(True)
        self.rightClick = False
        self.leftClick = False
        self.statusBar()

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('Fichier')
        fileMenu2 = menubar.addMenu('Outils')
        fileMenu3 = menubar.addAction('Readme')
        
        self.menu2= QMenuBar(menubar)
        menubar.setCornerWidget(self.menu2, Qt.TopRightCorner)
        fileMenu4 = self.menu2.addAction(" -- ")
        fileMenu5 = self.menu2.addAction(" // ")
        Exit = self.menu2.addAction(" X ")
        Exit.triggered.connect(app.exit)

        self.show()
    
    def mousePressEvent(self, event):
        super(App, self).mousePressEvent(event)

        if event.button() == Qt.RightButton:
            self.rdragx = event.x()
            self.rdragy = event.y()        
            self.currentx = self.width()
            self.currenty = self.height()
            self.rightClick = True
        
        if event.button() == Qt.LeftButton:
            self.leftClick = True
            global X,Y
            X=event.pos().x()
            Y=event.pos().y()
    
    def mouseMoveEvent(self, event):
        super(App, self).mouseMoveEvent(event)
        
        width = self.frameGeometry().width()
        height = self.frameGeometry().height()
        width5 = width + 5
        widthm5 = width - 5
        height5 = height + 5
        heightm5 = height - 5

        posMouse = event.pos()
        posMouseX = event.x()
        posMouseY = event.y()
        
        if posMouseX >= widthm5 and posMouseX <= width5:
         QApplication.setOverrideCursor(Qt.SizeHorCursor)
        elif posMouseX >= -5 and posMouseX <= 5:
         QApplication.setOverrideCursor(Qt.SizeHorCursor)
        elif posMouseY >= heightm5 and posMouseY <= height5:
         QApplication.setOverrideCursor(Qt.SizeVerCursor)
        elif posMouseY >= -5 and posMouseY <= 5:
         QApplication.setOverrideCursor(Qt.SizeVerCursor)
        else:
         QApplication.restoreOverrideCursor()

        if self.rightClick == True:
           
            x = max(self.widget.minimumWidth(), 
                self.currentx + event.x() - self.rdragx)
            y = max(self.widget.minimumHeight(), 
                self.currenty + event.y() - self.rdragy)
            self.resize(x, y)
        
        if self.leftClick == True: 
            self.move(event.globalPos().x()-X-X2,event.globalPos().y()-Y-Y2)      

    def mouseReleaseEvent(self, event):
        super(App, self).mouseReleaseEvent(event)
        self.rightClick = False
        self.leftClick = False

    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
Thank you in advance,

WB
Reply


Messages In This Thread
Drag and Move window from menubar - by WBPYTHON - Apr-03-2020, 11:49 AM
RE: Drag and Move window from menubar - by WBPYTHON - Apr-03-2020, 03:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 609 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] tkinter menubar not showing on mac ventura taras 1 2,123 Dec-17-2022, 02:44 PM
Last Post: Yoriz
  [Tkinter] MenuBar hidden when new window opens john8888 5 1,476 Sep-14-2022, 02:32 PM
Last Post: john8888
  why my list changes to a string as I move to another window in tkinter? pymn 4 2,613 Feb-17-2022, 07:02 AM
Last Post: pymn
  GUI with drag and drop functionality sayyedkamran 7 6,154 May-26-2020, 10:20 PM
Last Post: jefsummers
  Looking for Python IDE With Drag and Drop GUI Dan_PanMan 0 2,346 May-23-2020, 04:39 PM
Last Post: Dan_PanMan
  click drag olivers 1 2,959 Jan-30-2020, 01:44 AM
Last Post: Larz60+
  tkinter window and turtle window error 1885 3 6,794 Nov-02-2019, 12:18 PM
Last Post: 1885
  [PyQt] Drag items across tabs Alfalfa 5 4,845 Sep-01-2019, 11:58 PM
Last Post: Alfalfa
  [PyQt] Drag and drop converter Raures 0 4,521 Oct-01-2017, 07:44 PM
Last Post: Raures

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020