For now I'm loading them like this:
if __name__ == '__main__':
app = QApplication(sys.argv)
loader = QUiLoader()
file = QFile('main.ui')
file.open(QFile.ReadOnly)
window = loader.load(file)
file.close()
window.show()
# Here:
window.centralwidget.findChild(QListWidget, 'listWidget').addItems(['Item {0}'.format(x) for x in range(100)])
sys.exit(app.exec_())
But I think it's uncomfortably, is there any other way, probably to load whole namespace or whatever?
UPDATE:
The original solution below was written for PySide (Qt4). It still works with both PySide2 (Qt5) and PySide6 (Qt6), but with a couple of provisos:
loadUi
is called.(In addition, it should be mentioned that PySide2 and PySide6 now have a loadUiType function, which at first glance seems to provide a much simpler solution. However, the current implementation has the major drawback of requiring the Qt uic tool to be installed on the system and executable from the user's PATH. This certainly isn't always guaranteed to be the case, so it's debateable whether it's suitable for use in a production environment).
Below is an updated demo that illustrates the two features noted above:
test.py:
main.ui:
Original Solution:
At the moment, the PySide QUiLoader class doesn't have a convenient way to load widgets into to an instance of the top-level class like the PyQt uic module has.
However, it's fairly easy to add something equivalent:
Which could then used like this:
For this to work properly, the
baseinstance
argument ofloadUi
has to be an instance of the top-level class from Qt Designer file. All the other widgets will then be added to it as instance attributes.