Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Unsolved Using custom objects in widget-based QML

    QML and Qt Quick
    2
    5
    751
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      gkavrecic last edited by

      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 {}
          BoxInFile{}
      }
      

      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 https://forum.qt.io/topic/86789/using-a-qml-file-at-runtime

      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @gkavrecic last edited by

        @gkavrecic
        this is pretty well documented.
        Please read this.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        G 1 Reply Last reply Reply Quote 2
        • G
          gkavrecic last edited by

          @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 1 Reply Last reply Reply Quote 0
          • G
            gkavrecic @raven-worx last edited by

            @raven-worx

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

            1 Reply Last reply Reply Quote 0
            • raven-worx
              raven-worx Moderators @gkavrecic last edited by raven-worx

              @gkavrecic
              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
              

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply Reply Quote 0
              • First post
                Last post