Following a few tutorials, I've managed to pull together this small Python program that successfully pulls in an .ui
file and shows it on screen:
from PySide2.QtWidgets import QApplication
from PySide2.QtWidgets import QDialog, QMessageBox, QVBoxLayout
from PySide2.QtWidgets import QPushButton, QLineEdit
from PySide2.QtCore import QFile, Slot
from PySide2.QtUiTools import QUiLoader
class Dialog(QDialog):
def __init__(self, parent = None):
super(Dialog, self).__init__(parent)
print('hey')
def alert(self):
print('Alert')
if __name__ == '__main__':
app = QApplication([])
loader = QUiLoader()
loader.registerCustomWidget(Dialog)
ui_file = QFile('alert-quit.ui')
ui_file.open(QFile.ReadOnly)
dialog = loader.load(ui_file)
ui_file.close()
dialog.show()
The dialog shows correctly, but I get the error QObject::connect: No such slot QDialog::alert()
and the button does nothing. (The hey
text is not shown either.)
The .ui
file contains the definition of a QDialog
with a signal from the "Alert" button:
I'm not sure about what the registerCustomWidget()
, from another reply it seemed to be the thing to do. Sadly, the official documentation fails what how circle.ui
contains.
And the official documentation on loading .ui
files does not show how to interact with the items defined in the .ui
file itself.
How can I load a full QDialog
from an .ui
file and get the the buttons in it to trigger actions in my code?
P.S.: Since I cannot attach an .ui
file, here is its XML:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>344</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QPushButton" name="alert_button">
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Alert</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="quit_button">
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Quit</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>alert_button</sender>
<signal>clicked()</signal>
<receiver>Dialog</receiver>
<slot>alert()</slot>
<hints>
<hint type="sourcelabel">
<x>91</x>
<y>30</y>
</hint>
<hint type="destinationlabel">
<x>133</x>
<y>51</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>alert()</slot>
<slot>quit()</slot>
</slots>
</ui>