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. Can I combine QExplicitlySharedDataPointer and QObject?
Forum Updated to NodeBB v4.3 + New Features

Can I combine QExplicitlySharedDataPointer and QObject?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 371 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by
    #1

    I'd like to create a shared object using the QExplicitlySharedDataPointer setup, but I also want to use signals and slots to listen for changes to my shared data object.

    For example, modifying the example code from the docs a little, is there a way to modify this so that I can connect() a listener to the EmployeeData that will let me know when 'id' or 'name' changes? I don't think I can set that on Employee, since new Employee objects are wrappers around a pointer and are recreated every time I pss them by value:

    class EmployeeData : public QSharedData
    {
        int _id;
        QString _name;
        
      public:
        EmployeeData() : _id(-1) { }
        EmployeeData(const EmployeeData &other)
            : QSharedData(other), _id(other._id), _name(other._name) { }
        ~EmployeeData() { }
    
        int id() const { return _id; }
        void setId(int id) 
        {
            if (_id == id)
                return;
            _id = id;
            idChanged();
        }
        
        int name() const { return _name; }
        void setName(QString name) 
        {
            if (_name == name)
                return;
            _name = name;
            nameChanged();
        }
        
      signals:
        void idChanged();
        void nameChanged();
    };
    
    class Employee
    {
      public:
        Employee() { d = new EmployeeData; }
        Employee(int id, const QString &name) {
            d = new EmployeeData;
            setId(id);
            setName(name);
        }
        Employee(const Employee &other)
              : d (other.d)
        {
        }
        
        int id() const { return d->id(); }
        void setId(int id) { d->setId(id); }
    
        QString name() const { return d->name(); }
        void setName(const QString &name) { d->setName(name); }
    
    
      private:
        QExplicitlySharedDataPointer<EmployeeData> d;
    };
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi

      Did you try

      class EmployeeData : public QObject, public QSharedData
      {
      Q_OBJECT
      ....

      1 Reply Last reply
      1
      • K Offline
        K Offline
        kitfox
        wrote on last edited by
        #3

        That's not going to work because the EmployeeData object is hidden from the rest of the program by being wrapped in the Employee class. However, I can't turn Employee into a QObject either since it will go out of scope almost as soon as I've finished using it.

        jsulmJ 1 Reply Last reply
        0
        • K kitfox

          That's not going to work because the EmployeeData object is hidden from the rest of the program by being wrapped in the Employee class. However, I can't turn Employee into a QObject either since it will go out of scope almost as soon as I've finished using it.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @kitfox said in Can I combine QExplicitlySharedDataPointer and QObject?:

          However, I can't turn Employee into a QObject either since it will go out of scope almost as soon as I've finished using it.

          I don't get it: why do you think you can't nake Employee a QObject? How is going out of scope related to base class?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved