QML Newbie. Trying to set a python list as a QML ComboBox. I created a small usage example. Note that in the real program, the python list is not hardcoded.
Python Class:
class Test(QObject):
def __init__(self,):
super().__init__()
@Slot(result=list)
def getList(self):
return ["one", "two", "three"]
main.py:
def main():
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
test = Test()
engine.rootContext().setContextProperty("Test", test)
engine.load(QUrl.fromLocalFile(os.path.join(os.path.dirname(__file__), 'main.qml')))
if not engine.rootObjects():
raise Exception("No qml objects were loaded!")
return app.exec_()
if __name__ == '__main__':
sys.exit(main())
QML component:
ComboBox {
id: combo
width: parent.width
model: Test.getList() // no idea what to write here
textRole: "fileName"
onAccepted: {
print(combo.currentText)
}
}
Thanks!