Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Using custom objects in widget-based QML
QtWS25 Last Chance

Using custom objects in widget-based QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 2 Posters 1.2k Views
  • 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 Offline
    G Offline
    gkavrecic
    wrote on last edited by
    #1

    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-worxR 1 Reply Last reply
    0
    • G gkavrecic

      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-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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
      2
      • G Offline
        G Offline
        gkavrecic
        wrote on last edited by
        #3

        @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-worxR 1 Reply Last reply
        0
        • raven-worxR raven-worx

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

          G Offline
          G Offline
          gkavrecic
          wrote on last edited by
          #4

          @raven-worx

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

          1 Reply Last reply
          0
          • G gkavrecic

            @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-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #5

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

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved