0

I was experimenting with Qt Designer and I noticed that there is an option to get Python code for the UI.

enter image description here

But when I run this code in Python IDE nothing happens.

Is this code not suppose to run the designed UI?

EDIT: Here is what the generated code looks like:

# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'designertfNTig.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *


class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(640, 480)
        self.verticalLayoutWidget = QWidget(Form)
        self.verticalLayoutWidget.setObjectName(u"verticalLayoutWidget")
        self.verticalLayoutWidget.setGeometry(QRect(179, 9, 251, 471))
        self.verticalLayout = QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.pushButton_2 = QPushButton(self.verticalLayoutWidget)
        self.pushButton_2.setObjectName(u"pushButton_2")

        self.verticalLayout.addWidget(self.pushButton_2)


        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)
    # setupUi

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
        self.pushButton_2.setText(QCoreApplication.translate("Form", u"PushButton", None))
    # retranslateUi
2
  • Can you give us a sample of what the exported Python code looks like? Most likely, it doesn't have a run loop/QApplication attached to it. Commented Mar 26, 2021 at 14:52
  • hi thanks for your comment i edited the OP , Commented Mar 26, 2021 at 15:03

2 Answers 2

3

That code is not intended to be "run". It's a base "form class" that has to be inherited or instantiated in order to build the actual UI on an existing QWidget instance.

What you're seeing is the same result you'd get by running the pyuic utility from the command line.

The easy way to test an Ui from python, is to create the base instance of a widget and use the imported form class to "build" it on the widget. For very simple situations, the following can be enough:

from ui_form import Ui_Form
from PySide2 import QtWidgets

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    widget = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(widget)
    widget.show()
    sys.exit(app.exec_())

This works fine, but doesn't really provide the extensibility a subclass could.
To provide that, there are two possibilities.

Single inheritance method:

The concept is very similar to the above, but allowing implementation of functions that need to interact with the signals.

from ui_form import Ui_Form
from PySide2 import QtWidgets

class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        self.ui.pushButton_2.clicked.connect(self.hello)

    def hello(self):
        print('hello')

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    widget = MyWidget()
    widget.show()
    sys.exit(app.exec_())

This is pretty straighforward, but has a small thus important drawback: every object is only accessible using self.ui.someObject.

Multiple inheritance method:

The result is very similar, but has the benefit of creating all objects as direct attribute of the instance (self, not self.ui).

from ui_form import Ui_Form
from PySide2 import QtWidgets

class MyWidget(QtWidgets.QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.pushButton_2.clicked.connect(self.hello)

    def hello(self):
        print('hello')

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    widget = MyWidget()
    widget.show()
    sys.exit(app.exec_())

Fundamentally, what happens is that MyWidget inherits from both the Qt class and the "form class" (which is a simple python object that has only an implemented method, setupUi).

As you can see, they are both very similar, but the multiple inheritance is usually the preferred way, as it's more simpler, and even if it's only "3 characters", it actually improves code readability.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to create a QApplication manually, as the generated code is only the interface part. Create a file called main.py beside the ui_form.py file and write:

import sys
from PySide2.QtWidgets import QApplication, QWidget
from ui_form import Ui_Form

app = QApplication(sys.argv)
form = Ui_Form()
form.setupUi(QWidget())
form.show()
sys.exit(app.exec_())

1 Comment

Note that the OP is not building a QDialog, but a simple QWidget.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.