I'm trying to show a widget (not a window) using a separate UI file generated by Qt Designer. I created a class that inherits from the generated
Ui_Formclass, but when I run my code, nothing shows up on screen. What am I doing wrong?
This is a really common stumbling block when you're first working with Qt Designer and Python. The good news is that the fix is straightforward once you understand what's going on.
The problem
Here's the kind of code that causes this issue:
import sys
from smoky_UI import Ui_Form
from PySide6 import QtWidgets
class MyQtApp(Ui_Form):
def __int__(self):
super().__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
qt_app = MyQtApp()
qt_app.show()
sys.exit(app.exec())
There are actually two separate issues hiding in this short snippet. Let's go through each one.
Missing the QWidget base class
When Qt Designer generates a UI file and you convert it to Python (using pyside6-uic or pyuic6), the resulting class — Ui_Form in this case — is just a plain Python class. It contains a setupUi() method that creates and arranges all your widgets, but it doesn't inherit from any Qt widget class itself.
That means if your class only inherits from Ui_Form, it won't have any of the behavior that makes a Qt widget actually work — things like .show(), .resize(), or the ability to be drawn on screen. Your object simply isn't a widget.
To fix this, your class needs to inherit from both Ui_Form and a Qt widget class. Since you're building a form (not a main window), QWidget is the right choice:
class MyQtApp(QtWidgets.QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
By putting QtWidgets.QWidget first in the inheritance list, your class becomes a proper widget. The Ui_Form class then provides the setupUi() method, which populates that widget with all the controls you designed in Qt Designer.
If your .ui file was designed using a QMainWindow template in Qt Designer, you'd inherit from QtWidgets.QMainWindow instead:
PyQt/PySide Development Services — Stuck in development hell? I'll help you get your project focused, finished and released. Benefit from years of practical experience releasing software with Python.
class MyQtApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
The pattern is always the same: Qt widget class + generated UI class.
The __init__ typo
The second issue is a subtle typo. In the original code, the constructor is named __int__ (with one i) instead of __init__ (with two i's). Python won't raise an error for this — it will happily create a method called __int__, which is actually the magic method used for converting an object to an integer. It has nothing to do with initialization.
Because __init__ was never defined, setupUi() never gets called, and none of your designed widgets get created. Always double-check that your constructor is spelled __init__.
The corrected code
Here's the complete, working version:
import sys
from smoky_UI import Ui_Form
from PySide6 import QtWidgets
class MyQtApp(QtWidgets.QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
qt_app = MyQtApp()
qt_app.show()
sys.exit(app.exec())
The changes from the original:
QtWidgets.QWidgetis now included as a base class, making the object a real widget that can be displayed.__int__is corrected to__init__, so the constructor actually runs and callssetupUi().
A complete example without a .ui file
If you'd like to verify this pattern works on your system without needing a .ui file, here's a self-contained example that manually creates a simple form widget:
import sys
from PySide6.QtWidgets import (
QApplication,
QWidget,
QVBoxLayout,
QLabel,
QLineEdit,
QPushButton,
)
class MyForm(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("My Widget Form")
layout = QVBoxLayout()
label = QLabel("Enter your name:")
layout.addWidget(label)
self.name_input = QLineEdit()
layout.addWidget(self.name_input)
greet_button = QPushButton("Greet")
greet_button.clicked.connect(self.greet)
layout.addWidget(greet_button)
self.greeting_label = QLabel("")
layout.addWidget(self.greeting_label)
self.setLayout(layout)
def greet(self):
name = self.name_input.text()
self.greeting_label.setText(f"Hello, {name}!")
if __name__ == "__main__":
app = QApplication(sys.argv)
form = MyForm()
form.show()
sys.exit(app.exec())
This creates a QWidget with a text input and a button — a standalone widget form, not a QMainWindow. It demonstrates the same principle: your class inherits from QWidget, calls super().__init__(), and then sets up its contents.
When to use QWidget vs. QMainWindow
A quick note on choosing the right base class:
- Use
QWidgetwhen you want a simple, lightweight form or panel. This is what Qt Designer creates when you choose the "Widget" template. - Use
QMainWindowwhen you need features like a menu bar, toolbars, a status bar, or dock widgets. This is what Qt Designer creates when you choose the "Main Window" template.
The base class you choose in your Python code should match whatever template you used in Qt Designer. If you designed a Form (which produces Ui_Form), pair it with QWidget. If you designed a MainWindow (which produces Ui_MainWindow), pair it with QMainWindow.
Further reading
If you're getting started with Qt Designer, take a look at the tutorial on creating dialogs with Qt Designer for a full walkthrough of designing, exporting, and loading .ui files. For a broader introduction to building interfaces in code, the creating your first PySide6 window and widgets tutorials are a great place to start. And if Python class inheritance feels unfamiliar, reviewing Python classes will help solidify the foundations you need for working with Qt.
PyQt/PySide Office Hours 1:1 with Martin Fitzpatrick — Save yourself time and frustration. Get one on one help with your projects. Bring issues, bugs and questions about usability to architecture and maintainability, and leave with solutions.