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. How to use *.qml from qrc?
Forum Updated to NodeBB v4.3 + New Features

How to use *.qml from qrc?

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 7 Posters 22.8k 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.
  • F Offline
    F Offline
    fisher
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on last edited by
      #2

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

      --
      Vasiliy

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fisher
          wrote on last edited by
          #4

          @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. :)

          1 Reply Last reply
          0
          • I Offline
            I Offline
            imrrk
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              zing0000
              wrote on last edited by
              #6

              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]

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                zing0000
                wrote on last edited by
                #7

                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) ?? )

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

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

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    zing0000
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      viktor.benei
                      wrote on last edited by
                      #10

                      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.

                      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