Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 244
Ahad aghapour Avatar asked Oct 03 '17 12:10

Ahad aghapour


2 Answers

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()
like image 73
eyllanesc Avatar answered Oct 22 '22 05:10

eyllanesc


The easiest way is to use the *.ui file directly in the python code, you don't need convert to *.py file every time you change the ui. you can use this pseudo code in your project.

# imports
from PyQt5 import uic

# load ui file
baseUIClass, baseUIWidget = uic.loadUiType("MainGui.ui")

# use loaded ui file in the logic class
class Logic(baseUIWidget, baseUIClass):
    def __init__(self, parent=None):
        super(Logic, self).__init__(parent)
        self.setupUi(self)
         .
         .
         .
         .

def main():
    app = QtWidgets.QApplication(sys.argv)
    ui = Logic(None)
    ui.showMaximized()
    sys.exit(app.exec_())
like image 26
Ahad aghapour Avatar answered Oct 22 '22 03:10

Ahad aghapour