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. QMap to index QObject-derived objects
Forum Updated to NodeBB v4.3 + New Features

QMap to index QObject-derived objects

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 507 Views 2 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.
  • B Offline
    B Offline
    BaroneAshura
    wrote on 22 Oct 2024, 16:30 last edited by
    #1

    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?

    P 1 Reply Last reply 22 Oct 2024, 16:47
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 22 Oct 2024, 16:41 last edited by
      #2

      Don't store QObjects as values in any container. It will not work.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      B 1 Reply Last reply 22 Oct 2024, 16:47
      2
      • C Christian Ehrlicher
        22 Oct 2024, 16:41

        Don't store QObjects as values in any container. It will not work.

        B Offline
        B Offline
        BaroneAshura
        wrote on 22 Oct 2024, 16:47 last edited by
        #3

        @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

        C 1 Reply Last reply 22 Oct 2024, 16:50
        0
        • B BaroneAshura
          22 Oct 2024, 16:30

          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?

          P Offline
          P Offline
          Pl45m4
          wrote on 22 Oct 2024, 16:47 last edited by Pl45m4
          #4

          @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 a Person in your QMap:

          QMap<QString, Person * > some_people;
          

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          B 1 Reply Last reply 22 Oct 2024, 16:51
          0
          • B BaroneAshura
            22 Oct 2024, 16:47

            @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

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 22 Oct 2024, 16:50 last edited by
            #5

            @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.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • P Pl45m4
              22 Oct 2024, 16:47

              @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 a Person in your QMap:

              QMap<QString, Person * > some_people;
              
              B Offline
              B Offline
              BaroneAshura
              wrote on 22 Oct 2024, 16:51 last edited by
              #6

              @Pl45m4 said in QMap to index QObject-derived objects:

              Instead of storing the Person object, store a pointer to a Person in your QMap:

              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.

              P 1 Reply Last reply 22 Oct 2024, 16:58
              0
              • B Offline
                B Offline
                BaroneAshura
                wrote on 22 Oct 2024, 16:52 last edited by BaroneAshura
                #7

                thanks @Christian-Ehrlicher and @Pl45m4 for the responses.

                I will tag the question as solved

                1 Reply Last reply
                0
                • B BaroneAshura has marked this topic as solved on 22 Oct 2024, 16:52
                • B BaroneAshura
                  22 Oct 2024, 16:51

                  @Pl45m4 said in QMap to index QObject-derived objects:

                  Instead of storing the Person object, store a pointer to a Person in your QMap:

                  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.

                  P Offline
                  P Offline
                  Pl45m4
                  wrote on 22 Oct 2024, 16:58 last edited by
                  #8

                  @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.


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply
                  0

                  1/8

                  22 Oct 2024, 16:30

                  • Login

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