0

I want to create a GUI for a python file I wrote, so I downloaded PyQt5 and Qt Designer. When I exported my first try of the Qt Designer project I realized that the not PyQt5 was used but PySide2. What is the reason for this?

Till then everything worked fine, and I wanted to start some specific things like bringing a widget I created into a scrollArea. For this I created a custom widget and created a .ui and a .py. But they don't show up if I try to call them. Also the generated code for python looks like this:

from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint, QRect, QSize, QUrl, Qt)

from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont, QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap, QRadialGradient)

from PySide2.QtWidgets import *


class Ui_Form(object):
    def setupUi(self, Form):
    if Form.objectName():
        Form.setObjectName(u"Form")
    Form.resize(640, 480)
    self.widget = QWidget(Form)
    self.widget.setObjectName(u"widget")
    self.widget.setGeometry(QRect(270, 190, 77, 44))
    self.verticalLayout = QVBoxLayout(self.widget)
    self.verticalLayout.setObjectName(u"verticalLayout")
    self.verticalLayout.setContentsMargins(0, 0, 0, 0)
    self.label = QLabel(self.widget)
    self.label.setObjectName(u"label")

    self.verticalLayout.addWidget(self.label)

    self.pushButton = QPushButton(self.widget)
    self.pushButton.setObjectName(u"pushButton")

    self.verticalLayout.addWidget(self.pushButton)


    self.retranslateUi(Form)

    QMetaObject.connectSlotsByName(Form)
# setupUi

def retranslateUi(self, Form):
    Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
    self.label.setText(QCoreApplication.translate("Form", u"TextLabel", None))
    self.pushButton.setText(QCoreApplication.translate("Form", u"PushButton", None))
# retranslateUi

As you can see, my widget does not inherit from "QWidget" but from "object". Also, my widget has no "init" but a "setupUi". Why is that?

If I change to inherit from "object" to "QWidget" the code runs bot the widget does not show up. Also, when I try to show only the widget itself with the following code, it does not show up.

if __name__ == "__main__":
    import sys

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

Empty Widget

I tried to search around and did not find anything that helped me out, so I'm asking my questions here.

Can somebody please explain to me why all this is happening like it is right now and how I can make them widgets show up and add them to a scrollArea?

Thanks in advance!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Ferry
  • 23
  • 4

1 Answers1

0

The reason for having PySide instead of PyQt is that you used the pyside-uic tool instead if pyuic, which is a tool that creates very similar files but using PyQt.

Both PySide and PyQt are python bindings to Qt, and they work almost seamlessly, except from some implementation differences; this means two important things:

  1. they cannot be used together;
  2. most of the times you can just change the import statement at the beginning of each script to switch between them (from PyQt5 import... instead of from PySide2 import...);

They also have different licenses, so you need to check that if you want to create a commercial program.

About the created object, that is what is generally called a "form class", and it's used to "set up" a Qt widget instance: it's not a widget, but some sort of utility.

In order to properly use those generated scripts (which should never be manually modified), you must import them and then use them as a separate instance that will create the UI or as a base class for multiple inheritance, which is the most common method; the following example assumes that you used pyuic (not pyside-uic) to generate a file named mywidget.py, and creates a QWidget using the multiple inheritance:

from PyQt5 import QtWidgets
from mywidget import Ui_Form

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


if __name__ == "__main__":
    import sys

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

Read more about this in the official guidelines about using Designer.
Some more insight can be found in the answers to this related post

musicamante
  • 41,230
  • 6
  • 33
  • 58