Results 1 to 3 of 3

Thread: access rootContext() from Qt Resources QML

  1. #1
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default access rootContext() from Qt Resources QML

    Hello to everybody,
    I'm a newbie with QML and QT but I like it much!
    I'm trying to develop a funny application which must access database, acquire data from Modbus TCP server and display it on QML page.

    I'm developing different part separatly, and I've create the interface e some of back-end components, now, my problem is: how paste all into an executable?
    I'm using Microsoft Win XP and Win 7 to deploy my application.

    Reading QML documentation I found how create Qt Resources from QML files, so I can include them into executable to prevent errors or damage on .qml files. Now I try to create some method to update the field I inserted into QML files.

    My main.qml file contains some other qml component accessed by QtResource.

    My problem is: I need to expose C++ methods to QML interfaces located into QtResouces path.

    I have this code:
    Qt Code:
    1. class DeclarativeView : public QDeclarativeView
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit DeclarativeView(QWidget *parent = 0);
    7. ~DeclarativeView();
    8. QTimer * tim;
    9. Q_PROPERTY (double myValue READ readMyValue);
    10. double myVal;
    11. void Init();
    12. private slots:
    13. void updateValues();
    14.  
    15. };
    To copy to clipboard, switch view to plain text mode 
    the main element is Init function and readMyValue function
    Qt Code:
    1. void DeclarativeView::Init(){
    2.  
    3. setFocusPolicy(Qt::StrongFocus);
    4. setResizeMode(QDeclarativeView::SizeRootObjectToView);
    5. setSource(QUrl("qrc:/Sezione1.qml"));
    6. rootContext()->setContextProperty("window",this);
    7. }
    8.  
    9. double DeclarativeView::readMyValue(){
    10.  
    11. return myVal;
    12. }
    To copy to clipboard, switch view to plain text mode 

    ... but when I run my application I got this message:
    qrc:/Sezione1.qml:13: ReferenceError: Can't find variable: window
    How can access the rootContext from qrc environment?

    Is this the right way to do this?

    My objective is have only one executable which could access QML files from internal resources .qrc files, but also using Tcp Socket to acquire data and sql model to access database by C++ codes.

    Thanks for your time!

    Michele

  2. #2
    Join Date
    Sep 2009
    Location
    Finland
    Posts
    63
    Thanks
    1
    Thanked 22 Times in 19 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: access rootContext() from Qt Resources QML

    Swap the lines 5 and 6 in the latter part of your code and see whether it makes any difference. I remember seeing a comment in the documentation that first define context properties and then set the source file and your code you seem to do it in the opposite order.

  3. #3
    qttre Guest

    Default Re: access rootContext() from Qt Resources QML

    thanks,very much,It's very detail..........

Similar Threads

  1. Is it possible to add QT resources to a DLL?
    By cboles in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2017, 00:12
  2. MP3 file from Resources :-/
    By qtpat in forum Qt Programming
    Replies: 5
    Last Post: 26th March 2014, 16:34
  3. Replies: 16
    Last Post: 11th October 2009, 17:30
  4. Qt Resources
    By kaushal_gaurav in forum Qt Programming
    Replies: 3
    Last Post: 3rd October 2008, 16:30
  5. i need pyqt resources
    By slhtn in forum Newbie
    Replies: 2
    Last Post: 13th August 2006, 18:52

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.