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 508 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.
  • BaroneAshuraB Offline
    BaroneAshuraB Offline
    BaroneAshura
    wrote on 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?

    Pl45m4P 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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

      BaroneAshuraB 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

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

        BaroneAshuraB Offline
        BaroneAshuraB Offline
        BaroneAshura
        wrote on 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

        Christian EhrlicherC 1 Reply Last reply
        0
        • BaroneAshuraB BaroneAshura

          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?

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on 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

          BaroneAshuraB 1 Reply Last reply
          0
          • BaroneAshuraB BaroneAshura

            @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

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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
            • Pl45m4P 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 a Person in your QMap:

              QMap<QString, Person * > some_people;
              
              BaroneAshuraB Offline
              BaroneAshuraB Offline
              BaroneAshura
              wrote on 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.

              Pl45m4P 1 Reply Last reply
              0
              • BaroneAshuraB Offline
                BaroneAshuraB Offline
                BaroneAshura
                wrote on 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
                • BaroneAshuraB BaroneAshura has marked this topic as solved on
                • BaroneAshuraB BaroneAshura

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

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on 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

                  • Login

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