Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Properties are not accessible
Forum Updated to NodeBB v4.3 + New Features

[Solved] Properties are not accessible

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    StephanWoebbeking
    wrote on last edited by
    #1

    Hi,

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

    @
    #ifndef AUTOSTATEMACHINE_H
    #define AUTOSTATEMACHINE_H

    #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 MS_PER_DAY = 24 * SECOND_PER_MINUTE * MINUTE_PER_HOUR * MS_PER_SECOND;
    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_OBJECT
    Q_PROPERTY( int switchKnown READ getSwitchKnown )
    Q_PROPERTY( int currentState READ getCurrentState WRITE setCurrentState )
    Q_PROPERTY( int count READ getCount )
    public:
    AutoStateMachine();
    // 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 );
    private:
    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();
    };

    #endif // AUTOSTATEMACHINE_H
    @

    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,
    Stephan

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mmoll
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        StephanWoebbeking
        wrote on last edited by
        #3

        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?

        Stephan

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            StephanWoebbeking
            wrote on last edited by
            #5

            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?

            Stephan

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                StephanWoebbeking
                wrote on last edited by
                #7

                Thanks for the answers!

                Stephan

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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 :)

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved