QMap to index QObject-derived objects
-
wrote on 22 Oct 2024, 16:30 last edited by
I would like to have a QMap to index instances of a QObject-derived class.
code might look as simple as the following:
class Person : public QObject { Q_OBJECT public: }; void test() { QMap<QString, Person> some_people; Person weirdo; some_people["weirdo"] = weirdo; // compilation error some_people.insert("weirdo", weirdo); // no error }
I just found out that QObject has copy assignment explicitly deleted, preventing the insertion through the '=' operator; the insert method instead gives no compilation issue.
I would like to just understand the rationale behind the different compilation constraints in case of copy assignment operator and the insert function.
Should I be aware of potential side effects? Should I just avoid having QMaps of QObject-derived objects?
-
Don't store QObjects as values in any container. It will not work.
-
Don't store QObjects as values in any container. It will not work.
wrote on 22 Oct 2024, 16:47 last edited by@Christian-Ehrlicher thanks for the quick response :)
Somehow I was suspecting that it would never work, given that copy assignment operator is explicitly deleted.
could you please direct me to some bits of documentation highlighting why it will not work?
Thanks in advance
-
I would like to have a QMap to index instances of a QObject-derived class.
code might look as simple as the following:
class Person : public QObject { Q_OBJECT public: }; void test() { QMap<QString, Person> some_people; Person weirdo; some_people["weirdo"] = weirdo; // compilation error some_people.insert("weirdo", weirdo); // no error }
I just found out that QObject has copy assignment explicitly deleted, preventing the insertion through the '=' operator; the insert method instead gives no compilation issue.
I would like to just understand the rationale behind the different compilation constraints in case of copy assignment operator and the insert function.
Should I be aware of potential side effects? Should I just avoid having QMaps of QObject-derived objects?
wrote on 22 Oct 2024, 16:47 last edited by Pl45m4@BaroneAshura said in QMap to index QObject-derived objects:
I just found out that QObject has copy assignment explicitly deleted
Yes, for many reasons.
Objects/"members" of the Meta-Object system have to be unique. They are identified by their object name, their properties and their place in their parent-child hierarchy ("Object-Tree").
Having two identical objects a.k.a. clones or copies is not possible.
It would break the whole system and therefore is not allowed.Instead of storing the
Person
object, store a pointer to aPerson
in yourQMap
:QMap<QString, Person * > some_people;
-
@Christian-Ehrlicher thanks for the quick response :)
Somehow I was suspecting that it would never work, given that copy assignment operator is explicitly deleted.
could you please direct me to some bits of documentation highlighting why it will not work?
Thanks in advance
@BaroneAshura said in QMap to index QObject-derived objects:
could you please direct me to some bits of documentation highlighting why it will not work?
Because your insert() does also not work - the object is not copyable.
-
@BaroneAshura said in QMap to index QObject-derived objects:
I just found out that QObject has copy assignment explicitly deleted
Yes, for many reasons.
Objects/"members" of the Meta-Object system have to be unique. They are identified by their object name, their properties and their place in their parent-child hierarchy ("Object-Tree").
Having two identical objects a.k.a. clones or copies is not possible.
It would break the whole system and therefore is not allowed.Instead of storing the
Person
object, store a pointer to aPerson
in yourQMap
:QMap<QString, Person * > some_people;
wrote on 22 Oct 2024, 16:51 last edited by@Pl45m4 said in QMap to index QObject-derived objects:
Instead of storing the
Person
object, store a pointer to aPerson
in yourQMap
:I was considering that when I stumbled in the deleted operator.
yet I was really surprised though, that going through the call QMap::insert yields no compilation error... thus confusing me.
-
wrote on 22 Oct 2024, 16:52 last edited by BaroneAshura
thanks @Christian-Ehrlicher and @Pl45m4 for the responses.
I will tag the question as solved
-
-
@Pl45m4 said in QMap to index QObject-derived objects:
Instead of storing the
Person
object, store a pointer to aPerson
in yourQMap
:I was considering that when I stumbled in the deleted operator.
yet I was really surprised though, that going through the call QMap::insert yields no compilation error... thus confusing me.
wrote on 22 Oct 2024, 16:58 last edited by@BaroneAshura said in QMap to index QObject-derived objects:
going through the call QMap::insert yields no compilation error... thus confusing me
Only because it does not crash immediately it does not mean that it's correct :)
Probably it will crash sooner or later when you try to access the
Person
in your map and/or the "blueprint" outside the map.
1/8