Reputation: 141
I am having difficulties to load a widget generated by Qt Designer into my MainWindow. It fails when I have also defined signals and slots in the UI file.
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtWidgets
from PyQt5 import uic as pyuic
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args):
super().__init__(*args)
self.uifile = 'serialTandemGUI.ui'
self.form_widget = pyuic.loadUi(self.uifile)
_widget = QtWidgets.QWidget()
_layout = QtWidgets.QVBoxLayout(_widget)
_layout.addWidget(self.form_widget)
self.setCentralWidget(_widget)
if __name__ == '__main__':
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
w = MainWindow()
w.show()
app.exec_()
The error I get is:
AttributeError: 'QWidget' object has no attribute 'init'
Is it possible at all to inherit a widget this way? Tia!
Edit: Here's a simple UI file with a clicked() signal to demonstrate. It works as long as no signal definitions have been made (i.e. really just UI, but that is only half of the work, or even less).
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>GroupBox</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>Form</receiver>
<slot>init()</slot>
<hints>
<hint type="sourcelabel">
<x>203</x>
<y>162</y>
</hint>
<hint type="destinationlabel">
<x>396</x>
<y>201</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>init()</slot>
</slots>
</ui>
Upvotes: 1
Views: 1299
Reputation: 244282
Explanation:
When you have created the .ui you have only pointed out that there is a connection between the clicked signal and the init
method of the widget, but when you load it using loadUi()
and do not pass it as a second parameter a QWidget will use the base class of the design, in your case QWidget, which clearly has no "init" method throwing you that error.
Solution:
You must create a class that inherits from QWidget and has the init
method, and then use loadUi()
to fill it out.
# -*- coding: utf-8 -*-
import os
import sys
from PyQt5 import QtCore, QtWidgets, uic as pyuic
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
current_dir = os.path.dirname(os.path.realpath(__file__))
uifile = os.path.join(current_dir, "serialTandemGUI.ui")
pyuic.loadUi(uifile, self)
@QtCore.pyqtSlot()
def init(self):
print("init")
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.form_widget = Widget()
_widget = QtWidgets.QWidget()
_layout = QtWidgets.QVBoxLayout(_widget)
_layout.addWidget(self.form_widget)
self.setCentralWidget(_widget)
if __name__ == "__main__":
app = QtWidgets.QApplication.instance() or QtWidgets.QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Upvotes: 2