Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Out of range condition on Q_PROPERTY
Qt 6.12 LTS is out! See what's new in the release blog

Out of range condition on Q_PROPERTY

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.4k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I am new to QT and was wondering how are you supposed to handle errors in the QT property system, more specifically on set the functions?

    class SomeClass : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(qint16 temperature READ temperature WRITE setTemperature)

    private:
    qint16 mTemperature;
    ...
    ...
    ...

    void SomeClass::temperature()
    {
    return mTemperature;
    }

    void SomeClass::setTemperature(qint16 _temperature)
    {
    if (_temperature < 10 || _temperature > 100)
    {
    // argument is out of range, what do you do here?
    return;
    }
    mTemperature = _temperature;
    }

    Are you supposed to throw an exception, invoke a signal or what?
    Any help would be greatly appreciated.

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

      HI and welcome to devnet,

      Are you supposed to throw an exception, invoke a signal or what?

      For sure Qt does not use exceptions.

      What usually I do (is not an official Qt guideline) is to force the value to be in the range; you have 2 options depending of what do you need:

      1. Don't change the value if the proposed value is out-of range;
      void MyObject::setValue (int newValue) 
      {
          if (newValue < MIN_VALUE || newValue > MAX_VALUE)
              return;
      
          if (_v != newValue) {
              _v = newValue;
      
              Q_EMIT valueChanged(_v);
          }
      }
      
      1. Force the value to the limit values.
      void MyObject::setValue (int newValue) 
      {
          if (newValue < MIN_VALUE)
              newValue = MIN_VALUE;
      
          if (newValue > MAX_VALUE)
              newValue = MAX_VALUE;
      
          if (_v != newValue) {
              _v = newValue;
      
              Q_EMIT valueChanged(_v);
          }
      }
      

      Anyway if you have a range (or only one of lower/upper limit) I suggest to define READ ONLY properties for the limits

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Mcosta,

        Thanks for the input, I'll probably use coercion as you suggest seems like the logical thing to do.

        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
        • Unsolved