Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

python - QtDesigner changes will be lost after redesign User Interface

I'm using Qt Designer for design GUI to use in python, after designing my desired UI in Qt Designer, convert it to python code and then I changed generated code to do some action in my python code, but if I changed the UI with Qt Designer and convert it to python code again, I lost my previous changes on my code.

how can I solve the problem?

can we Spreading a Class Over Multiple Files in python to write code in other files?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To avoid having these problems it is advisable not to modify this file but to create a new file where we implement a class that uses that design.

For example, suppose you have used the MainWindow template in the design.ui file, then convert it to Ui_Design.py like to the following structure:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        [...]

    def retranslateUi(self, MainWindow):
        [...]

Then we will create a new file that we will call logic.py where we will create the file that handles the logic and that uses the previous design:

class Logic(QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)

So even if you modify the design and generate the file again .py you will not have to modify the file of the logic.

To generalize the idea we must have the following rules but for this the logic class must have the following structure:

class Logic(PyQtClass, DesignClass):
    def __init__(self, *args, **kwargs):
        PyQtClass.__init__(self, *args, **kwargs)
        self.setupUi(self)
  • PyQtClass: This class depends on the design chosen.

   Template                      PyQtClass
 ─────────────────────────────────────────────
   Main Window                   QMainWindow
   Widget                        QWidget 
   Dialog with Buttons Bottom    QDialog
   Dialog with Buttons Right     QDialog
   Dialog with Without Buttons   QDialog

  • DesignClass: The name of the class that appears in your design.

The advantage of this implementation is that you can implement all the logic since it is a widget, for example we will implement the solution closing pyqt messageBox with closeevent of the parent window :

class Logic(QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
    def closeEvent(self, event):
        answer = QtWidgets.QMessageBox.question(
            self,
            'Are you sure you want to quit ?',
            'Task is in progress !',
            QtWidgets.QMessageBox.Yes,
            QtWidgets.QMessageBox.No)
        if answer == QtWidgets.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...