Results 1 to 4 of 4

Thread: knowing when the value of an integer changes.

  1. #1
    Join Date
    Apr 2017
    Posts
    33
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default knowing when the value of an integer changes.

    I have a long unsigned int "error_word" where each bit represents an operation in my application. If the bit is set, that operation has had an error or failed and needs to be looked at.

    I realize that primatives do not have signals or events associated with them.

    Is there some other data type or object I can use that will send an event/signal that I can monitor. I currently have the code set up with a timer that calls a slot that compares the value of the current error_word to its previous value 2 seconds ago. I'm hoping someone in this forum has a suggestion

    Thanks

    -emp1953

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,817
    Thanks
    216
    Thanked 590 Times in 580 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: knowing when the value of an integer changes.

    As you know, only QObject-based classes can have signals or slots. My initial idea was to make a QObject class to hold the value of the variable you want to monitor, and override some of the basic functions, something like this (untested code!):

    Qt Code:
    1. class MonitoredValue : public QObject
    2. {
    3. Q_OBJECT;
    4.  
    5. public:
    6. MonitoredValue( const uint32_t & initialVal, QObject * parent = 0 ) : QObject( parent ), monitoredVal( initialVal ) {}
    7. virtual ~MonitoredVal() {}
    8.  
    9. // read-only access
    10. const uint32_t & operator() const { return monitoredVal; }
    11. uint32_t & operator() const { return monitoredVal; }
    12.  
    13. // assignment
    14. const MonitoredValue & operator=( const uint32_t & newVal )
    15. { emit valueChanging( monitoredVal ); monitoredVal = newVal; emit valueChanged( monitoredVal ); return *this; }
    16.  
    17. const MonitoredValue & operator=( const MonitoredValue & rhs )
    18. { emit valueChanging( monitoredVal ); monitoredVal = rhs.monitoredVal; emit valueChanged( monitoredVal ); return *this; }
    19.  
    20. // cast operators
    21. operator uint32_t & () { return monitoredVal; }
    22. operator const uint32_t & () const { return monitoredVal; }
    23.  
    24. signals:
    25. void valueChanging( const uint32_t & val );
    26. void valueChanged( const uint32_t & val );
    27.  
    28. private:
    29. uint32_t monitoredVal;
    30. };
    To copy to clipboard, switch view to plain text mode 

    Example:

    Qt Code:
    1. using ErrorWord = MonitoredValue;
    2.  
    3. ErrorWord errorWord( 0 );
    4. connect( &errorWord, &ErrorWord::valueChanging, this, &MyClass::errorChanging );
    5. connect( &errorWord, &ErrorWord::valueChanged, this, &MyClass::errorChanged );
    6.  
    7. uint32_t currentError = errorWord;
    8.  
    9. errorWord = 42; // assigns new error, emits signals;
    To copy to clipboard, switch view to plain text mode 

    It would be nice if you could create this as a template, so that the monitored value could be any type, but Qt doesn't permit that for QObject classes. It would also be nice if you could connect any member function of a class to a slot, but as far as I can tell, the signal / slot mechanism requires that the class emitting the signal be derived from QObject.

    And because Qt also doesn't support copy constructors or assignment operators for QObject classes, this method may not work without some modification.

    You can also use other signal / slot mechanisms, like Boost Signals2. This might work better for your purpose than Qt's version.
    Last edited by d_stranz; 2nd December 2018 at 18:20.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2017
    Posts
    33
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: knowing when the value of an integer changes.

    Quote Originally Posted by d_stranz View Post
    As you know, only QObject-based classes can have signals or slots. My initial idea was to make a QObject class to hold the value of the variable you want to monitor, and override some of the basic functions, something like this (untested code!):
    Thank you for that, I'll give it a go.

    emp1953

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,817
    Thanks
    216
    Thanked 590 Times in 580 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: knowing when the value of an integer changes.

    Please report back and tell us how it goes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Can I use Qt without knowing programming?
    By heiwu in forum Qt Programming
    Replies: 2
    Last Post: 25th November 2013, 19:16
  2. knowing screen coordinate
    By riarioriu3 in forum Qt Programming
    Replies: 3
    Last Post: 10th July 2012, 09:38
  3. knowing mouse location
    By riarioriu3 in forum Qt Programming
    Replies: 2
    Last Post: 21st June 2012, 09:48
  4. qprogressbar without knowing the time
    By jaca in forum Qt Programming
    Replies: 3
    Last Post: 25th September 2009, 23:45
  5. Knowing if application is running
    By mourad in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2008, 11:47

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.