Congratulations to our 2022 Qt Champions!

Using custom objects in widget-based QML

  • I have a Widget based application and one part is showing a QML file.
    It works fine with built in types.

    Want to load an external QML file (e.g. myScreen.qml) with custom QML objects.
    Let's says there are two custom objects:

    • BoxInResources (it shall be contained in the qml.qrc)
    • BoxInFile (it shall be contained in a file named BoxInFile.qml)

    The myScreen.qml shall look something like:

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    Rectangle {
        color: "green"
        Text { text: "TEST" }
        BoxInResources {}

    First, I've tried with the BoxInResources. Added it in source files, qml.qrc, as QML. But nothing did the trick.
    Each time at load it replies: BoxInResource is not a type

    I also get the error if the file is in the working path.
    How can I link it to the executable so it will be available as a type?

    This is a rephrased question of

  • Moderators

    this is pretty well documented.
    Please read this.

  • @raven-worx¸

    Thanks for that, but the link describes integrating C++ objects into QML.
    This is the next step.

    I would like to use QML objects. (hence the BoxInFile part)
    I managed to add BoxInResource in a pure QML application.

  • @raven-worx

    I was more thinking of this approach.
    But can't make it work.

  • Moderators

    So you can either import the folder containing your type/component directly from qrc within QML:

    import "qrc:/path/containing/components"

    If it's not working try: import ":/path/containing/components"

    or alternatively:

    // C++
    qmlRegisterType(QUrl("qrc:///path/to/MyComponent.qml")), "MyTypes", 1, 0, "MyComponent");
    // QML
    import MyTypes 1.0

Log in to reply