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. Wrapper with signals for primitive types

Wrapper with signals for primitive types

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 389 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.
  • G Offline
    G Offline
    G_ka
    wrote on last edited by
    #1

    Hi,
    I'd like to create a wrapper for primitive types, that would emit a signal when the value is changed and have a slot to change the value.The obvious way would be to use a template class:

    template<typename Type>
    class QValueWrapper : public QObject
    {
        Q_OBJECT
    
    signals:
        void valueChanged(Type newValue);
    
    public:
        void setValue(const Type &newValue)
        {
            if(value_ != newValue)
                emit valueChanged(newValue);
            value_ = newValue;
        }
    
    private: 
        Type value_;
    };
    

    But Qt signals does not support template classes. So how would you do it? I'd like to avoid creating a wrapper for each value type, if possible
    Thanks

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #3

      But Qt signals does not support template classes.

      this is not strictly correct. It's MOC that can't cope with templates.

      You have 3 alternatives that I can think of:

      • QVariant as correctly pointed out by @mcleary
      • bypass MOC using Verdigris
      #include <wobjectdefs.h>
      #include <wobjectimpl.h>
      template<typename Type>
      class QValueWrapper : public QObject
      {
          W_OBJECT(QValueWrapper )
      public:
          QValueWrapper(QObject* parent = nullptr) :QObject(parent){};
          QValueWrapper(const Type & val,QObject* parent = nullptr) : QObject(parent), value_(val){}
          void valueChanged(Type newValue);
          W_SIGNAL(valueChanged, newValue)
          const Type & value() const {return value_;}
          void setValue(const Type &newValue)
          {
              if(value_ != newValue)
                  emit valueChanged(newValue);
              value_ = newValue;
          }
          W_SLOT(setValue)
      private: 
          Type value_;
      };
      W_OBJECT_IMPL(QValueWrapper <Type>, template <typename Type>)
      
      • move the signal to a template-less qobject
      class QValueWrapperHelper : public QObject
      {
          Q_OBJECT
      public:
      QValueWrapperHelper(QObject* parent = nullptr) :QObject(parent){};
      signals:
          void valueChanged();
      };
      
      template<typename Type>
      class QValueWrapper : public QValueWrapperHelper 
      {
      public:
      QValueWrapper(QObject* parent = nullptr) :QValueWrapperHelper(parent){};
      QValueWrapper(const Type & val,QObject* parent = nullptr) :QValueWrapperHelper(parent), value_(val){};
      const Type & value() const {return value_;}
          void setValue(const Type &newValue)
          {
              if(value_ != newValue)
                  emit valueChanged();
              value_ = newValue;
          }
      private: 
          Type value_;
      };
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      7
      • M Offline
        M Offline
        mcleary
        wrote on last edited by
        #2

        The first thing that comes to my mind is to use a QVariant to store the value you want. QVariant supports all primitive types.

        1 Reply Last reply
        5
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #3

          But Qt signals does not support template classes.

          this is not strictly correct. It's MOC that can't cope with templates.

          You have 3 alternatives that I can think of:

          • QVariant as correctly pointed out by @mcleary
          • bypass MOC using Verdigris
          #include <wobjectdefs.h>
          #include <wobjectimpl.h>
          template<typename Type>
          class QValueWrapper : public QObject
          {
              W_OBJECT(QValueWrapper )
          public:
              QValueWrapper(QObject* parent = nullptr) :QObject(parent){};
              QValueWrapper(const Type & val,QObject* parent = nullptr) : QObject(parent), value_(val){}
              void valueChanged(Type newValue);
              W_SIGNAL(valueChanged, newValue)
              const Type & value() const {return value_;}
              void setValue(const Type &newValue)
              {
                  if(value_ != newValue)
                      emit valueChanged(newValue);
                  value_ = newValue;
              }
              W_SLOT(setValue)
          private: 
              Type value_;
          };
          W_OBJECT_IMPL(QValueWrapper <Type>, template <typename Type>)
          
          • move the signal to a template-less qobject
          class QValueWrapperHelper : public QObject
          {
              Q_OBJECT
          public:
          QValueWrapperHelper(QObject* parent = nullptr) :QObject(parent){};
          signals:
              void valueChanged();
          };
          
          template<typename Type>
          class QValueWrapper : public QValueWrapperHelper 
          {
          public:
          QValueWrapper(QObject* parent = nullptr) :QValueWrapperHelper(parent){};
          QValueWrapper(const Type & val,QObject* parent = nullptr) :QValueWrapperHelper(parent), value_(val){};
          const Type & value() const {return value_;}
              void setValue(const Type &newValue)
              {
                  if(value_ != newValue)
                      emit valueChanged();
                  value_ = newValue;
              }
          private: 
              Type value_;
          };
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          7
          • G Offline
            G Offline
            G_ka
            wrote on last edited by G_ka
            #4

            Thanks to both of you. I think I will use your third solution, @VRonin
            I'll mark this topic as solved as soon as I try it

            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