Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML switch not working as expected with bind c++ property
Forum Updated to NodeBB v4.3 + New Features

QML switch not working as expected with bind c++ property

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.6k 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.
  • B Offline
    B Offline
    bludger
    wrote on last edited by
    #1

    Hi all,

    Suppose I've a c++ object (derived from QObject) which exposes a property using the Q_PROPERTY macro. Whenever I bind this property to the checked property of a QML switch it reacts properly to the notify signal (changes from c++ code are properly propagated). But when I change the checked state of the switch by clicking on it, it will not call the respective set function of the bind property.

    Some code to reproduce it:

    CppObject.h

    class CppObject : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
    
    public:
        explicit CppObject(QObject *parent = nullptr) {}
    
        bool active() const { return m_active; }
        void setActive(bool active) { if (m_active != active) { m_active = active; emit activeChanged(); } }
    
    signals:
        void activeChanged();
    
    private:
        bool m_active;
    
    };
    

    Part of main.ccp

        CppObject obj;
        engine.rootContext()->setContextProperty("cppObject", &obj);
    
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    

    main.qml

    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Switch {
            id: switch2
            checked: cppObject.active
        }
    }
    

    I'm using Qt 5.9.1 and QtQuick.Controls 2.2. Does someone experience the same behaviour?

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      Bindings are not bidirectional. If you want switch2.checked =>cppObject.active as well as cppObject.active => switch2.checked, the connection needs to be explicitly stated.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      1
      • B Offline
        B Offline
        bludger
        wrote on last edited by
        #3

        Thanks for the clarification @jeremy_k. I was always under the impression that property binding was bidirectional, mainly due to the Q_PROPERTY read and write parameters/functions. I think however that it is a bit lame that it can automatically read values by the getter but lacks the setter functionality.

        jeremy_kJ 1 Reply Last reply
        0
        • B bludger

          Thanks for the clarification @jeremy_k. I was always under the impression that property binding was bidirectional, mainly due to the Q_PROPERTY read and write parameters/functions. I think however that it is a bit lame that it can automatically read values by the getter but lacks the setter functionality.

          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by jeremy_k
          #4

          @bludger said in QML switch not working as expected with bind c++ property:

          I think however that it is a bit lame that it can automatically read values by the getter but lacks the setter functionality.

          The setter isn't missing. The program[mer] has to tell the runtime what to connect the input of that setter to. It's no less automatic than the getter, which also requires an explicit reference to the property.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          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