Home > Backend Development > Python Tutorial > How Can I Update My Qt Designer UI Without Losing My Code Changes?

How Can I Update My Qt Designer UI Without Losing My Code Changes?

Mary-Kate Olsen
Release: 2024-12-18 06:15:09
Original
432 people have browsed it

How Can I Update My Qt Designer UI Without Losing My Code Changes?

QtDesigner UI Updates Without Overwriting Changes

If you've encountered the issue where UI changes made in Qt Designer are later overwritten after code conversion, this article provides a solution to preserve your modifications.

To address this problem, avoid modifying the generated Python code directly. Instead, create a separate class in a new file to handle the UI logic. This class should inherit from the design class generated by Qt Designer and implement its methods.

Example:

Consider a MainWindow template with a corresponding design.ui file. Convert it to Ui_Design.py as follows:

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

    def retranslateUi(self, MainWindow):
        [...]
Copy after login

Create a new file logic.py:

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

By following these rules, you can modify the design and regenerate its code without affecting your logic implementation. Ensure that your logic class adheres to the following structure:

class Logic(PyQtClass, DesignClass):
    def __init__(self, *args, **kwargs):
        PyQtClass.__init__(self, *args, **kwargs)
        self.setupUi(self)
Copy after login

Where:

  • PyQtClass depends on the design template (e.g., QMainWindow, QWidget).
  • DesignClass is the name of the class in your design (e.g., Ui_MainWindow).

Additional Benefits:

This implementation allows you to implement logic within a designated widget, like closing a pyqt messageBox with the parent window's closeEvent:

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()
Copy after login

The above is the detailed content of How Can I Update My Qt Designer UI Without Losing My Code Changes?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template