How to use *.qml from qrc?



  • When I try to use qml in qt app, I get an error: Label is not a type.

    main.cpp
    @
    #include <QApplication>
    #include <QDeclarativeView>
    #include <QDeclarativeContext>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QDeclarativeView view;
    view.setSource(QUrl("qrc:/main.qml"));
    view.show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    qml.qrc:
    @
    <RCC>
    <qresource prefix="/">
    <file>main.qml</file>
    <file>label.qml</file>
    </qresource>
    </RCC>
    @

    main.qml:
    @
    import Qt 4.7

    Item {
    id: main

    width: 480
    height: 800
    
    Rectangle{color:"blue"; width: 100; height: 100;}
    
    Label{text:"111"}
    

    }
    @

    label.qml:
    @
    import Qt 4.7

    Text {
    smooth: true
    font.pixelSize: 24
    font.family: "Calibri"
    font.bold: true
    }
    @

    It seems qml cannot find another qml file from qrc. But, if I use Loader element, it can work fine:

    main.qml:
    @
    // ...
    Loader {
    id: pageLoader
    source:"label.qml"

        onLoaded:{
            pageLoader.item.text = "123"
        }
    }
    // ...
    

    @

    en ..... what happened?



  • try
    @ source:"qrc:/label.qml"@
    or
    @ source:":/label.qml"@



  • Hi,

    The label.qml file will need to be capitalized (i.e. Label.qml) if you want to use it as a type from QML (http://doc.qt.nokia.com/4.7-snapshot/qml-extending-types.html#defining-new-components).

    Regards,
    Michael



  • @Vass

    In Qt 4.7 doc, that have a paragraph:

    Note that the resource system cannot be accessed from QML directly. If the main QML file is loaded as a resource, all files specifed as relative paths in QML will also be loaded from the resource system. Using the resource system is completely transparent to the QML layer. This also means that if the main QML file is not loaded as a resource then files in the resource system cannot be accessed from QML.
    http://doc.qt.nokia.com/4.7/qtbinding.html

    So I think it shouldn't be used "qrc...".

    @mbrasser
    Very accurate solution! Thank you very much. :)



  • hi fisher:
    specify the path correctly..
    ex:
    "qrc:/new/prefix/(qml file name)"



  • to fisher

    I have the same problem and reduced it to the almost identical simple case that you provide. I think your code is correct, except for the upper case Label.qml filename as pointed out by mbrasser.

    However, even with that change, I still get "Label is not a type".

    Have you got it working?

    Some additional info:

    • if I comment out
      @Label{text:"111"}@ in main.qml
      everything is OK and the code,
      @QObject object = (QObject)view.rootObject();@
      delivers the root object.
    • if I look at qrc_project.cpp in GeneratedFiles, it seems to be OK with
      @static const unsigned char qt_resource_struct[] = {
      ...
      }@
      that shows all the resources correctly.

    [Edit: code formatting; mlong]



  • It gets stranger!
    "bug report ":http://bugreports.qt.nokia.com/browse/QTBUG-12916

    Using this code in a new VS2008 project works. Somewhere there is an environment problem but I haven't found it yet.
    (why cannot I attach a ZIP of the project in this forum reply (only 7kB) ?? )



  • [quote author="zing0000" date="1294601741"](why cannot I attach a ZIP of the project in this forum reply (only 7kB) ?? )[/quote]

    No uploads are allowed at all, not even images. You'll have to put it onto some other public server, sorry.



  • OK, it was a case-sensitive issue. QML component names must match exactly the filenames. It's not just a first letter capitalization, the whole name must match.



  • I tried a lot of different approaches and for me this is the easiest solution:

    I have a main.qml and a Button.qml in my resource file with prefix '/qml/views'. By default main.qml cannot use Button.qml, but if I add 'Button.qml' as alias for Button.qml it finds it and works properly.

    Tested on Windows (7 64bit) with Qt 4.8.1 (MSVC 2010)

    Note: it even works if the file is not capitalized correctly (e.g. 'button.qml') but the alias is ('Button.qml'), only the alias matters.


Log in to reply
 

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