Important: Please read the Qt Code of Conduct -

How to use *.qml from qrc?

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

    #include <QApplication>
    #include <QDeclarativeView>
    #include <QDeclarativeContext>

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

    QDeclarativeView view;
    return a.exec&#40;&#41;;


    <qresource prefix="/">

    import Qt 4.7

    Item {
    id: main

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


    import Qt 4.7

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

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

    // ...
    Loader {
    id: pageLoader

            pageLoader.item.text = "123"
    // ...


    en ..... what happened?

  • try
    @ source:"qrc:/label.qml"@
    @ 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 (


  • @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.

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

    Very accurate solution! Thank you very much. :)

  • hi fisher:
    specify the path correctly..
    "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 ":

    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