python - PyQt5 draggable frameless window -


i found example set borders on frameless window, it's not draggable. how can make frameless window draggable? if can see example it'll awesome. here example code(normally code longer, that's why there libraries don't mind them);

from pyqt5.qtwidgets import (qmessagebox,qapplication, qwidget, qtooltip, qpushbutton,                              qdesktopwidget, qmainwindow, qaction, qapp, qtoolbar, qvboxlayout,                              qcombobox,qlabel,qlineedit,qgridlayout,qmenubar,qmenu,qstatusbar,                              qtextedit,qdialog,qframe,qprogressbar                              ) pyqt5 import qtcore, qtwidgets, qtgui pyqt5.qtgui import qicon,qfont,qpixmap,qpalette pyqt5.qtcore import qcoreapplication, qt,qbasictimer  import sys  class cssden(qmainwindow):     def __init__(self):         super().__init__()           self.mwidget = qmainwindow(self)         self.setwindowflags(qtcore.qt.framelesswindowhint)           #size         self.setfixedsize(320, 450)         self.center           #label         self.lbl = qlabel(self)         self.lbl.settext("test")         self.lbl.setstylesheet("background-color: rgb(0,0,0);"                                "border: 1px solid red;"                                "color: rgb(255,255,255);"                                "font: bold italic 20pt 'times new roman';")         self.lbl.setgeometry(5,5,60,40)          self.show()      #center     def center(self):         qr = self.framegeometry()         cp = qdesktopwidget().availablegeometry().center()         qr.movecenter(cp)         self.move(qr.topleft())  app = qapplication(sys.argv) app.setstylesheet("qmainwindow{background-color: darkgray;border: 1px solid black}")  ex = cssden() sys.exit(app.exec_()) 

you need handle mouse events yourself.

  • we need add event on mousepressevent, keep place last clicked on window
  • then, add mousemoveevent, calculate distance between last clicked point , current mouse location. move window according distance.

this fixed code:

from pyqt5.qtwidgets import (qmessagebox,qapplication, qwidget, qtooltip, qpushbutton,                              qdesktopwidget, qmainwindow, qaction, qapp, qtoolbar, qvboxlayout,                              qcombobox,qlabel,qlineedit,qgridlayout,qmenubar,qmenu,qstatusbar,                              qtextedit,qdialog,qframe,qprogressbar                              ) pyqt5 import qtcore, qtwidgets, qtgui pyqt5.qtgui import qicon,qfont,qpixmap,qpalette pyqt5.qtcore import qcoreapplication, qt,qbasictimer, qpoint  import sys  class cssden(qmainwindow):     def __init__(self):         super().__init__()           self.mwidget = qmainwindow(self)         self.setwindowflags(qtcore.qt.framelesswindowhint)           #size         self.setfixedsize(320, 450)         self.center()           #label         self.lbl = qlabel(self)         self.lbl.settext("test")         self.lbl.setstylesheet("background-color: rgb(0,0,0);"                                "border: 1px solid red;"                                "color: rgb(255,255,255);"                                "font: bold italic 20pt 'times new roman';")         self.lbl.setgeometry(5,5,60,40)          self.oldpos = self.pos()          self.show()      #center     def center(self):         qr = self.framegeometry()         cp = qdesktopwidget().availablegeometry().center()         qr.movecenter(cp)         self.move(qr.topleft())      def mousepressevent(self, event):         self.oldpos = event.globalpos()      def mousemoveevent(self, event):         delta = qpoint (event.globalpos() - self.oldpos)         #print(delta)         self.move(self.x() + delta.x(), self.y() + delta.y())         self.oldpos = event.globalpos()  app = qapplication(sys.argv) app.setstylesheet("qmainwindow{background-color: darkgray;border: 1px solid black}")  ex = cssden() sys.exit(app.exec_()) 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -