Results 1 to 2 of 2

Thread: Problem with QML and C++ properties

  1. #1
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Problem with QML and C++ properties

    Hi everybody.

    I'm trying to load a QML component depending on the value of a certain variable declared as a property in a C++ class but when I start debugging the application QML does not recognize the C++ object containing that property.
    Does anyone can help? I'll put the code down here.

    This is the main.cpp file:

    Qt Code:
    1. #include <QtGui/QGuiApplication>
    2. #include <QtQml>
    3. #include "app.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QGuiApplication app(argc, argv);
    8.  
    9. cApp viewer;
    10. viewer.setMainQmlFile(QStringLiteral("qml/ClemDemo/main.qml"));
    11. viewer.showExpanded();
    12.  
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 


    This is the cApp class derived from the class generated by the QtQuick wizard in order to use a particular nativeEvent function:

    Qt Code:
    1. #include <QtQml>
    2. #include "qtquick2applicationviewer.h"
    3. #include "myclass.h"
    4.  
    5. class cApp : public QtQuick2ApplicationViewer
    6. {
    7. Q_OBJECT
    8.  
    9. bool nativeEvent(const QByteArray &eventType, void *message, long *result);
    10.  
    11. public:
    12. cMyClass* myClass ;
    13. explicit cApp(QWindow *parent = 0);
    14. virtual ~cApp();
    15.  
    16. public slots:
    17. void device_in();
    18. void device_out();
    19. };
    20.  
    21. cApp::cApp(QWindow *parent) : QtQuick2ApplicationViewer(parent)
    22. {
    23. myClass = new cMyClass;
    24. rootContext()->setContextProperty("myqmlclass", myClass );
    25. connect(myClass , SIGNAL(device_connected()), this, SLOT(device_in()));
    26. connect(myClass , SIGNAL(device_removed()), this, SLOT(device_out()));
    27. }
    28.  
    29. bool cApp::nativeEvent(const QByteArray &eventType, void *message, long *result)
    30. {
    31. return myClass ->nativeEvent(eventType, message, result);
    32. }
    33.  
    34. void cApp::device_in()
    35. {
    36. myClass ->setDeviceConnected(true);
    37. }
    38.  
    39. void cApp::device_out()
    40. {
    41. myClass ->setDeviceConnected(false);
    42. }
    To copy to clipboard, switch view to plain text mode 


    This is the class i need to use into QML:

    Qt Code:
    1. class cMyClass: public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. friend class cApp;
    6.  
    7. protected:
    8. bool nativeEvent(const QByteArray &eventType, void *message, long *result);
    9.  
    10. public:
    11. cMyClass();
    12. virtual ~cMyClass(){}
    13.  
    14. public:
    15. bool DeviceConnected;
    16. Q_PROPERTY(bool connected READ isConnected WRITE setDeviceConnected NOTIFY somethingHappened)
    17.  
    18. void setDeviceConnected(bool status){ DeviceConnected = status;}
    19. bool isConnected(){ return DeviceConnected; }
    20.  
    21. signals:
    22. void somethingHappened();
    23.  
    24. void device_connected();
    25. void device_removed();
    26. };
    27.  
    28. cMyClass::cMyClass()
    29. {
    30. //Connections for attach/detach notification of a particular device
    31. // .............
    32. }
    33.  
    34. bool cMyClass::nativeEvent(const QByteArray &eventType, void *message, long *result)
    35. {
    36. //Handle messages of device attached/detached
    37. }
    To copy to clipboard, switch view to plain text mode 


    And here is the QML code:

    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. BorderImage {
    4. id: main_window
    5. source: "qrc:/main/sfondo.jpg"
    6. width: 1680; height: 932
    7. horizontalTileMode: BorderImage.Stretch
    8. verticalTileMode: BorderImage.Stretch
    9.  
    10. Component
    11. {
    12. id: devices
    13. Rectangle {
    14. id: device_side
    15. width: 400; height: 500
    16. color: "#C0A3A3A3" // #AARRGGBB where AA is Alpha channel (for transparency)
    17. radius: 8
    18. }
    19. }
    20. Loader { id: device_loader }
    21. myqmlclass.onSomethingHappened: { device_loader.sourceComponent = devices }
    22. }
    To copy to clipboard, switch view to plain text mode 

    The error is that myqmlclass is not recognized as a QML property. This is what I can see on the application output:
    "Cannot assign to non-existent property "myqmlclass"
    myqmlclass.onSomethingHappened: { device_loader.sourceComponent = devices }"
    Last edited by anda_skoa; 14th March 2014 at 11:04. Reason: changed [qtclass] to [code]

  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: Problem with QML and C++ properties

    somethingHappend is a signal, not a property.

    You have two options:

    1) property binding
    Qt Code:
    1. Loader {
    2. id: device_loader
    3. sourceComponent = myqmlclass.connected ? devices : null
    4. }
    To copy to clipboard, switch view to plain text mode 

    2) Connection element
    Qt Code:
    1. Connection {
    2. target: myqmlclass
    3. onSomethingHappend: ....
    4. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 25th September 2011, 14:44
  2. about properties
    By jajdoo in forum Newbie
    Replies: 5
    Last Post: 21st July 2011, 14:15
  3. QUiLoader Problem: No Dynamic Properties Support
    By kalli in forum Qt Programming
    Replies: 0
    Last Post: 28th June 2010, 21:51
  4. ViewPort Properties
    By Kapil in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 11:57
  5. QAxWidget Properties
    By ToddAtWSU in forum Newbie
    Replies: 1
    Last Post: 13th February 2006, 17:45

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
  •  
Qt is a trademark of The Qt Company.