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): [...]
Create a new file logic.py:
class Logic(QMainWindow, Ui_MainWindow): def __init__(self, *args, **kwargs): QMainWindow.__init__(self, *args, **kwargs) self.setupUi(self)
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)
Where:
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()
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!