First of all I recommend you read the PyQt docs referring to Qt Designer.
Going to the problem, Qt Designer does not provide a widget but a class that serves as an interface to a widget, and that can be seen in his statement:
class Ui_gen_settings(object):
# ...
The class inherits from object and not from QWidget, QDialog, QMainWindow, etc.
In the docs that indicate initially it is recommended to create a widget and use the interface provided by Qt Designer. For this it is correct to use pyuic but I will change the gen_settings.py to gen_settings_ui.py so that the change is understood.
pyuic5 gen_settings.ui -o gen_settings_ui.py
So now we create a file called gen_settings.py that contains the widget and use the interface.
gen_settings.py
from gen_settings_ui import Ui_gen_settings
from PyQt5 import QtWidgets
class Gen_Settings(QtWidgets.QWidget, Ui_gen_settings):
def __init__(self, parent=None):
super(Gen_Settings, self).__init__(parent)
self.setupUi(self)
Then when you create the .ui corresponding to Ui_MainWindow you add a QWidget that is an empty container.
In the image, the Widget container is the one in the upper left, and now we will replace it with Gen_Settings, so we must promote it using the following procedure:
- Right click on the widget container and select the
Promote To ...
option.
- The following Dialog will appear and fill in the fields as shown in the image (I'm assuming that gen_settings_ui.py and gen_settings.py are in the same folder as the current .ui)
- You press the Add button and then the Promote button.
Then you convert the .ui to .py with pyuic and you will get the following:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
# ...
self.widget = Gen_Settings(self.centralwidget)
self.widget.setObjectName("widget")
self.gridLayout.addWidget(self.widget, 0, 0, 1, 1)
# ...
from gen_settings import Gen_Settings
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…