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



  • 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!!!


  • Moderators

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



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



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



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



  • [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



  • 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 :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.