0

I've build a simple app layout with QtDesigner

pyside6-designer

Then i saved the .ui file and converted to python code:

pyuic6 -x simpleApp.ui -o simpleApp.py

At this point i decided to make the window Frameless and Draggable.

So i've write this line and it works:

Form.setWindowFlags(QtCore.Qt.WindowType.FramelessWindowHint)

Then i added this two functions to work with the Mouse Events:

def mousePressEvent(self, event):        
    self.dragPos = event.globalPosition().toPoint()

def mouseMoveEvent(self, event): 
    Form.move(Form.pos() + event.globalPosition().toPoint() - self.dragPos)
    self.dragPos = event.globalPosition().toPoint()
    event.accept()

Now the issue is that the Frameless line works, instaed the 2 functions for drag the window dosen't works. I searched also online some solution but nothing seems usefull. Any advice?

Here's the complete runnable code:

# Form implementation generated from reading ui file 'simpleApp.ui'
#
# Created by: PyQt6 UI code generator 6.4.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(320, 300)
        Form.setWindowFlags(QtCore.Qt.WindowType.FramelessWindowHint)   # <--- This line make Frameless Window
        self.label = QtWidgets.QLabel(parent=Form)
        self.label.setGeometry(QtCore.QRect(120, 70, 81, 16))
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(parent=Form)
        self.pushButton.setGeometry(QtCore.QRect(110, 120, 75, 24))
        self.pushButton.setObjectName("pushButton")
    
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "Im a Label"))
        self.pushButton.setText(_translate("Form", "PushButton"))

    def mousePressEvent(self, event):                                           # First Function
        self.dragPos = event.globalPosition().toPoint()

    def mouseMoveEvent(self, event):                                            # Second Function
        Form.move(Form.pos() + event.globalPosition().toPoint() - self.dragPos)
        self.dragPos = event.globalPosition().toPoint()
        event.accept()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec())
  • Those functions don't work because `Ui_Form` is a basic python class, and trying to override event handlers in it is pointless; in fact, you should not ever try to edit those files (as clearly written in the warning at the top). You need to subclass from QWidget in order to make those functions working. See the duplicated answer and look at the official PyQt guidelines about [using Designer](//www.riverbankcomputing.com/static/Docs/PyQt6/designer.html). You should also always use [layout managers](//doc.qt.io/qt-6/layout.html) (even [in Designer](https://doc.qt.io/qt-6/designer-layouts.html)). – musicamante Aug 28 '23 at 11:29

0 Answers0