Results 1 to 9 of 9

Thread: How to use QML in C++ application

  1. #1
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default How to use QML in C++ application

    Hi

    We have developed a calculator in Qt Quick which consists of a few files:
    1) A few .png files
    2) 3 QML files
    3) 1 Javascript file

    It works perfectly in the Qt QML Viewer. Now we are trying to use this in our main application. We added all these files to a .qrc resource file and tried to show the main file like this:

    Qt Code:
    1. QDeclarativeView view = new QDeclarativeView;
    2. view->setSource(QUrl("qrc:/qml/Calculator.qml"));
    3. view->show();
    To copy to clipboard, switch view to plain text mode 

    This shows a blank view. We also tried to load the file from our hard drive in which case it shows, but it does not find any images etc.

    Any suggestions on how this must be done will be appreciated.
    Thanks
    Jaco

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QML in C++ application

    Is this the exact code? Does it compile? I'm asking because line #1 is missing an asterisk.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: How to use QML in C++ application

    Hi

    Sorry that was a typo. In the application we are using a d-pointer which contains the view. I removed this to make the example easier to understand.

    Some progress information:
    1) I managed to get it working from my hard drive. When I copied the files from the place where it was developed into the resource folder I moved the images and that is why they did not load. When I fixed it it works fine. This is the code:

    Qt Code:
    1. QDeclarativeView* view = new QDeclarativeView;
    2. view->setSource(QUrl::fromLocalFile("D:/Work/Calculator/resources/qml/Calculator.qml"));
    3. view->show();
    To copy to clipboard, switch view to plain text mode 

    2) However trying to get it to work out of the qt resource system still displays an empty white view.

    Thanks,
    Jaco

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QML in C++ application

    How do you refer to the other files that are called from within your qml file?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: How to use QML in C++ application

    Just by their names. For example:

    Qt Code:
    1. Image {
    2. anchors.fill: parent
    3. smooth: false
    4. source: "background.png"
    5. fillMode: Image.Stretch
    6. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QML in C++ application

    You have to tell Qt they are contained within the resource system.

    javascript Code:
    1. Image {
    2. anchors.fill: parent
    3. smooth: false
    4. source: "qrc:/background.png"
    5. fillMode: Image.Stretch
    6. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: How to use QML in C++ application

    Thank you that makes sense. I've changed it but I can still not see if it is working because when I try to load the main .qml file from the resource system it does not show. This is how I set the source.

    Qt Code:
    1. view->setSource(QUrl("qrc:/qml/Calculator.qml"));
    To copy to clipboard, switch view to plain text mode 

    I believe this is the correct way to construct a QUrl for something in the resource system because I found this in the documentation for QTextBrowser:
    If you want to load documents stored in the Qt resource system use qrc as the scheme in the URL to load. For example, for the document resource path :/docs/index.html use qrc:/docs/index.html as the URL with setSource().
    Am I missing something?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QML in C++ application

    Make sure those files are really compiled into the resource system. Try listing all files under "qrc:/" directory (e.g. using QDir).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    JPNaude (9th December 2010)

  10. #9
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: How to use QML in C++ application

    Ok I solved it!

    While stepping into the setSource() function with the debugger I realized it was printing something to the app output. It could not find the javascript file since the case sensitivity was wrong, in the emulator it worked but not in the view.

    One thing to note was that the images only worked when it did not have the qrc:/ infront of it.

Similar Threads

  1. Replies: 8
    Last Post: 5th November 2012, 08:43
  2. Replies: 2
    Last Post: 21st November 2010, 18:03
  3. Replies: 3
    Last Post: 20th October 2010, 22:36
  4. Replies: 3
    Last Post: 6th January 2010, 16:55
  5. Replies: 12
    Last Post: 29th February 2008, 13:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.