Unsolved Can I combine QExplicitlySharedDataPointer and QObject?
-
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; };
-
Hi
Did you try
class EmployeeData : public QObject, public QSharedData
{
Q_OBJECT
.... -
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.
-
@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?