0

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!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • change to `@Slot(result="QVariantList")` and remove `textRole: "fileName"` – eyllanesc Feb 03 '20 at 15:45
  • @eyllanesc Thanks for the answer, I'm having import issues with QVariantType from PySide2. what exactly do I need to import in order to use this class? – Yosi Kadoshi Feb 09 '20 at 07:31

0 Answers0