converting ui file into py using pyside6

Viewed 2404

I just try pyside6 to convert ui files into py files.

When I was using pyside2, I was writing this commande line to convert file:

pyside2-uic MainWindow.ui -o ui_mainwindow.py -x

But with pyside6, it is not working anymore: the "-x" doesn't look necessary. So you have to write:

pyside6-uic MainWindow.ui -o ui_mainwindow.py

BUT, when I run the new file generated, nothing happend. I had a look at the end of file, and it is missing a part there was with pyside2. This part of code is not here anymore:

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

What I am doing wrong ?

2 Answers

You're not doing anything wrong, that part of the code is added to create an "executable" version of the pyuic file, and its purpose is mainly to test it.

I don't know if the support for -x has been dropped, but that makes sense, and it won't matter a lot anyway: pyuic generated files are not intended to be executed, and should not ever be manually modified, since they should only be used as imports as explained in the official guidelines about using Designer.

The way I do it is import the class the uic created:

from ui_mainwindow import Ui_MainWindow

Then I create a QMainWindow class and instantiate the uic class. You need to call the setupUi method on the instance.

class UI(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    ...

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    win = UI()
    win.show()
    app.exec()

Now, you can access all the widgets via self.ui:

self.ui.myPushButton.clicked.connect(self.doSomething)
Related