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. Call a function every time a variable is changed
Forum Updated to NodeBB v4.3 + New Features

Call a function every time a variable is changed

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.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.
  • T Offline
    T Offline
    tobben24
    wrote on last edited by
    #1

    Hi,

    Is it possible to automatically call a function every time a specified variable is changed?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      shifty
      wrote on last edited by
      #2

      I believe there is a way if it is a "property" inside a QML component, then you get a onVariableNameChanged and you can call whatever you would like there

      Ex:

      property string myVar: "default_value"

      onMyVarChanged: { // call your function here }

      Hope this helps!!

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        No, there is not. If you want that, then you should make your variable private to a class, and create a public accessor method for it. So:
        @
        //old situation
        public:
        int value;

        //somewhere in code
        value = 10;

        //new situation:
        private:
        int value;
        public:
        void setValue(int newValue) {value = newValue; /* whatever else you need to do*/}

        //somewhere in code:
        setValue(10);
        @

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobben24
          wrote on last edited by
          #4

          Okay. Thanks for the replies:) I think I will go with the last one.

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

            Oh, haha one was using QML, the other Qt C++,

            If you want to go the C++ route there is this page "Qt Bindings":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html

            Which may help you... I believe they use signals inside the C++ code as well as using the Q_PROPERTY macro

            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