Home > Software engineering >  Use QML object from another QML file
Use QML object from another QML file

Time:10-10

Let's say I have the following files:

main.py

app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.quit.connect(app.quit)
engine.load("qml/mainMenu.qml")
sys.exit(app.exec())

main.qml

ApplicationWindow{
    width: 1200
    height: 800
    ...

    CustomObject{...}

customobject.qml

CustomObject{...}

Now as you can see I want to use CustomObject in main.qml. I couldn't find out how to import (or whatever I have to do) both files so I can do that.

CodePudding user response:

If main.qml is in the same directory as CustomObject, then you don't need to import anything at all. If it's not working, it is because you need to rename customobject.qml to be CustomObject.qml. See the docs. At least the first letter must be upper-case, and the object's name becomes the same as the filename. (This can actually be overridden in a qmldir file though).

  • Related