[Solved] Properties are not accessible

  • Hi,

    I just wanted to try out the properties functionality and thought something like this should be working but, surprisingly, it's not... ;)


    #include "statemachinenode.h"

    #include <QObject>
    #include <QDateTime>
    #include <QList>

    const int MS_PER_SECOND = 1000;
    const int SECOND_PER_MINUTE = 60;
    const int MINUTE_PER_HOUR = 60;
    const int NO_AUTO_SWITCH = 0;
    const int STATE_ON = 2;
    const int STATE_OFF = 1;
    const int STATE_IDLE = 0;

    class AutoStateMachine : public QObject {
    Q_PROPERTY( int switchKnown READ getSwitchKnown )
    Q_PROPERTY( int currentState READ getCurrentState WRITE setCurrentState )
    Q_PROPERTY( int count READ getCount )
    // property int currentState: READ getCurrentState() WRITE setCurrentState();
    int timeToSwitchMS();
    //property bool switchKnown: getSwitchKnown()
    bool checkSwitch();
    void setNode( int idx, int state, int followState, int timeoutMSs );
    void addNode( int state, int followState, int timeoutMSs );
    int getCurrentState();
    void setCurrentState( int state );
    QDateTime activationTimestamp;
    //pNodes: array of TStateMachineNode;
    QList<StateMachineNode *> pNodes;
    bool pSwitchKnown;
    int pCurrentState;
    StateMachineNode *getStateMachineNode( int idx );
    bool getSwitchKnown();
    int findNodeIdx( int state );
    int getCount();


    Then in the using class (a test case) I try to access like this:

    void TestAutoStateMachine::automaticSwitch() {
    AutoStateMachine *machine;
    qDebug() << "Automatic switch goes";

    machine = new AutoStateMachine();
    QVERIFY2( machine != 0, "State machine was not instantiated!" );
    machine->addNode( 1, 2, 1000 );
    machine->addNode( 2, 1, 1000 );
    machine->currentState = 1;
    QVERIFY2( machine->count == 2, "There are " + machine->count + " states in the machine not the expected 2!" );


    but get these errors:

    D:\DataSource\Qt\ADNW_Service\ServiceToolsSharedQt\Test\ServiceToolsSharedQt_Test\testautostatemachine.cpp:44: Fehler: 'class AutoStateMachine' has no member named 'currentState'
    machine->currentState = 1;

    D:\DataSource\Qt\ADNW_Service\ServiceToolsSharedQt\Test\ServiceToolsSharedQt_Test\testautostatemachine.cpp:45: Fehler: 'class AutoStateMachine' has no member named 'count'
    QVERIFY2( machine->count = 2, "There are " + machine->count + " states in the machine not the expected 2!" );

    Anyone any idea why this is happening? I am working on Win 7 Pro, SP1, Qt 3.1.1 on Qt 5.2.1.

    Thanks in advance,

  • You need to access Qt properties using QObject::property() and QObject::setProperty()

  • Ok.... I find these macros being used everywhere in the web, why can't I use these? How would I use the ones you named?


  • Lifetime Qt Champion


    You are using them right, but your use of currentState is wrong.

    To be more clear "currentState" is the name of your property. It's for the meta object system. If you want to access your properties, you have two choices:

    property, setProperty like mmoll suggested

    getCurrentState and setCurrentState

    Hope it helps

  • Ok, I think it aids to my yet little understanding of the overall system. So I was expecting to have shortcut access by the source code via the properties, as I tried in the code, something like "object.property = xyz" without the longer way via the setter functions. But from the information you gave me I now reckon that these properties are mainly for things like the visual editor which grabs the properties by an automatic mechanism and shows them to the programmer to easily adjust them, is that true? And the way via setProperty() is more or less a by-product and the "right" way of accessing this data by code is still by using the setter/getter methods?


  • Lifetime Qt Champion

    No, you can also use them for meta programming, introspection etc. It's also used a lot in QML. Most user however will use the getters/setters, it all depends on your needs.

  • Thanks for the answers!


  • Lifetime Qt Champion

    You're welcome !

    If that answers all your questions then please update the thread title prepending [solved] so other forum users may know a solution has been found :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.