Results 1 to 2 of 2

Thread: Accessing QGuiApplication derived class properties from QML

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Accessing QGuiApplication derived class properties from QML

    I am trying to pass an instance of Application class, which is derived from QGuiApplication, to QML and access its properties there. For the reasons I don't understand it tells the property is undefined: "Unable to assign [undefined] to QString". At the same time properties inherited from QGuiApplication can be accessed without any problem, e.g. app.applicationName works fine.

    Here is the QML code (main.qml). The Text component was supposed to show the application version.
    Qt Code:
    1. import QtQuick 2.7
    2. import QtQuick.Controls 2.0
    3.  
    4. ApplicationWindow
    5. {
    6. title: "another title"
    7. visible: true
    8. width: 1000
    9. height: 800
    10.  
    11. Rectangle
    12. {
    13. color: "lightsteelblue"
    14. anchors.fill: parent
    15.  
    16. Text
    17. {
    18. anchors.centerIn: parent
    19. //text: app.updatesAvailable ? "Updates Are Available" : "My Application"
    20. //text: app.updatesAvailable
    21. text: app.versionString
    22. //text: app.applicationName
    23. //text: testObj.title
    24. }
    25. } // Rectangle
    26. } // ApplicationWindow
    To copy to clipboard, switch view to plain text mode 

    This is how the application instance is passed to QML (main.cpp):
    Qt Code:
    1. #include "application.h"
    2. #include <QQmlContext>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. Application app(argc, argv);
    7.  
    8. QQmlApplicationEngine engine;
    9. engine.rootContext()->setContextProperty("app", &app);
    10. engine.load(QUrl("qrc:///qml/main.qml"));
    11.  
    12. return app.start();
    13. }
    To copy to clipboard, switch view to plain text mode 

    And, finally, the Application class declaration (application.h) looks like this:
    Qt Code:
    1. #ifndef APPLICATION_H
    2. #define APPLICATION_H
    3.  
    4. #include <QGuiApplication>
    5.  
    6. class Application: public QGuiApplication
    7. {
    8. Q_PROPERTY(bool updatesAvailable READ checkForUpdates)
    9. Q_PROPERTY(QString versionString READ getVersionString CONSTANT)
    10.  
    11. public:
    12. Application(int &argc, char **argv);
    13. ~Application();
    14.  
    15. bool isRunning() const { return false; }
    16. bool checkForUpdates() { return true; }
    17.  
    18. QString getVersionString() const { return "2.7"; }
    19.  
    20. int start();
    21. }; // Application
    22.  
    23. #endif // APPLICATION_H
    To copy to clipboard, switch view to plain text mode 

    Any clues?
    Magicians do not exist

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing QGuiApplication derived class properties from QML

    Missing the Q_OBJECT macro.

    Also properties exposed to QML need to be either CONSTANT or have an associated NOTIFY signal.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    mentalmushroom (31st August 2016)

Similar Threads

  1. Replies: 3
    Last Post: 6th April 2012, 17:44
  2. QGraphicsItem subclass and accessing custom properties
    By been_1990 in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2010, 02:48
  3. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 15:53
  4. Accessing a class Object by pointer in another class
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2008, 16:59
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 08:36

Tags for this Thread

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.