Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. C++ project use qml via qrc and doesn't work
Forum Updated to NodeBB v4.3 + New Features

C++ project use qml via qrc and doesn't work

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 2.9k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    lesner
    wrote on 26 Jun 2012, 07:06 last edited by
    #1

    here is my code:
    main.cpp
    @int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QDeclarativeView viewer;

    viewer.setSource(QUrl("qrc:/resource/QtJoystickUI.qml"));
    viewer.show();
    .
    .
    .

    return app.exec();
    }@

    qrc file:
    @<RCC>
    <qresource prefix="resource">
    <file alias ="QtJoystickUI.qml">QtJoystickUI.qml</file>
    <file alias ="Qpositionmoniter">Qpositionmoniter.qml</file>
    </qresource>
    </RCC>
    @

    QtJoystickUI:
    @Rectangle {
    id: base
    objectName: "base"
    width: 300
    height: 400
    color: "#00000000"
    .
    .
    .

    Qpositionmoniter{
        id: positionmoniter
        x: z_axis_text.x
        y: z_axis_text.y + 50
        width: 200
        height: 200
    }
    

    }@

    Qpositionmoniter:
    @Item {
    id: moniterBase
    Rectangle {
    id: base
    color: "gray"
    width: parent.width;
    height: parent.height
    }
    Rectangle {
    id: position
    color: "black"
    width: 20
    height: 20
    radius: width/2
    x: base.width/2 - radius
    y: base.height/2 - radius
    }
    }
    @

    seems the main.cpp can't get the qml file
    but if I turn QtJoystickUI into this:
    @Rectangle {
    id: base
    objectName: "base"
    width: 300
    height: 400
    color: "#00000000"
    .
    .
    .
    // Qpositionmoniter{
    // id: positionmoniter
    // x: z_axis_text.x
    // y: z_axis_text.y + 50
    // width: 200
    // height: 200
    // }
    }@

    everything is ok, the app's ui didn't go wrong
    I don't know why it happened, can anyone give some help?
    thanks for any help!!!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 26 Jun 2012, 07:18 last edited by
      #2

      Try adding import statement for "Qpositionmoniter", like this:
      @
      import "qrc:/resource"
      @

      (Z(:^

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 26 Jun 2012, 07:19 last edited by
        #3

        That should not be needed. URL's in QML files are relative.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SteveKing
          wrote on 26 Jun 2012, 07:46 last edited by
          #4

          You don't have a .qml extension in the alias for Qpositionmoniter in the QRC file.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 26 Jun 2012, 07:51 last edited by
            #5

            [quote author="SteveKing" date="1340696771"]You don't have a .qml extension in the alias for Qpositionmoniter in the QRC file. [/quote]
            Excellent catch!

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lesner
              wrote on 26 Jun 2012, 08:00 last edited by
              #6

              [quote author="SteveKing" date="1340696771"]You don't have a .qml extension in the alias for Qpositionmoniter in the QRC file. [/quote]

              Thanks a lot!!!
              seems I'm too careless

              1 Reply Last reply
              0
              • M Offline
                M Offline
                miroslav
                wrote on 28 Jun 2012, 12:39 last edited by
                #7

                One way to prevent that from happening again is adding
                @
                Q_ASSERT(QFile(<resource path>).exists());
                @

                before setting the URL source. Just make sure to use the same path later :-)

                Mirko Boehm | mirko@kde.org | KDE e.V.
                FSFE Fellow
                Qt Certified Specialist

                1 Reply Last reply
                0

                1/7

                26 Jun 2012, 07:06

                • Login

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