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. Inheritance QMap
Forum Updated to NodeBB v4.3 + New Features

Inheritance QMap

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 863 Views 3 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.
  • D Offline
    D Offline
    Deedss
    wrote on last edited by Deedss
    #1

    Hello,

    I've been wondering if it's possible to inherit from a QMap. For my application I use a QMap to send data to a GUI but also to a json file. Due to using a QMap, all keys are sorted alphabetically by default. For the GUI I need to control the order of the keys so that I can set certain Items at the top of the listView. My plan was to use a customMap and inherit from QMap in which there is a QStringList that also records the keys in order of insertion. In the header file I've defined the class

    public CustomMap : public QVariantMap
    

    The only difference compared to QMap is the insertion and removal from the QList.

    /*******************************************************************************
    *   Constructor & Destructor
    *******************************************************************************/
    CustomMap::CustomMap()
    {
    
    }
    
    CustomMap::~CustomMap() = default;
    
    /*******************************************************************************
    *   Public Functions
    *******************************************************************************/
    void CustomMap::insert(QString key, QVariant value)
    {
        QMap::insert(key, value);
        m_list.append(key);
    }
    
    void CustomMap::remove(QString key)
    {
        QMap::remove(key);
        for (int i = 0; i < m_list.size(); i++) {
            if (m_list.at(i) == key) {
                m_list.removeAt(i);
            }
        }
    }
    
    QStringList CustomMap::keys()
    {
        return m_list;
    }
    
    
    

    Do you think this is possible?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Deedss said in Inheritance QMap:

      Do you think this is possible?

      Yes but it's nonsense - a map is a container whose keys are ordered (therefore you must provide 'bool operator <()' for non-trivial keys).

      Use a QSortFilterProxyModel if you want to show your data sorted in any way in your view.

      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
      4
      • D Offline
        D Offline
        Deedss
        wrote on last edited by
        #3

        The thing is that I use the map in several classes that are using inheritance.
        They all contain their specific parameters but in the view I want to show them per class. So first all parameters from firstcomp, then all parameters from secondcomp and last all parameters from lastcomp in a listview.

        BaseComp : Component : SmallComponent
        
        

        I also tried looking into using a QList<QPair> for it but was worried for the performance because I have to call a specific value quite a lot on an embedded device.

        kshegunovK 1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          While using a proxy model is the way to go for a view, you could also just cheat
          and use a container that keeps the insertion order if that is enough for you.
          https://github.com/Tessil/ordered-map

          Christian EhrlicherC 1 Reply Last reply
          0
          • D Deedss

            The thing is that I use the map in several classes that are using inheritance.
            They all contain their specific parameters but in the view I want to show them per class. So first all parameters from firstcomp, then all parameters from secondcomp and last all parameters from lastcomp in a listview.

            BaseComp : Component : SmallComponent
            
            

            I also tried looking into using a QList<QPair> for it but was worried for the performance because I have to call a specific value quite a lot on an embedded device.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @Deedss said in Inheritance QMap:

            So first all parameters from firstcomp, then all parameters from secondcomp and last all parameters from lastcomp in a listview.

            @Christian-Ehrlicher is right. Use a proxy model to reorder, sort or even modify the original data as you see fit. That's a typical case where aggregation is desired, not inheritance. Inheriting from QMap not only doesn't help you much, but is also very fragile. As for the performance, the point is moot, red-black trees are full of indirections and are not for performance-critical code. Not to mention that you have to prove first that this is a bottleneck, before worrying about it ...

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            3
            • mrjjM mrjj

              Hi
              While using a proxy model is the way to go for a view, you could also just cheat
              and use a container that keeps the insertion order if that is enough for you.
              https://github.com/Tessil/ordered-map

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @mrjj said in Inheritance QMap:

              and use a container that keeps the insertion order if that is enough for you.

              a.k.a. a vector

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

              mrjjM 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @mrjj said in Inheritance QMap:

                and use a container that keeps the insertion order if that is enough for you.

                a.k.a. a vector

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                well an associative container then :)
                In any case, the proxy would be much more flexible and not require to put a model on top of the
                ordered-map.

                kshegunovK 1 Reply Last reply
                0
                • mrjjM mrjj

                  @Christian-Ehrlicher
                  well an associative container then :)
                  In any case, the proxy would be much more flexible and not require to put a model on top of the
                  ordered-map.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @mrjj said in Inheritance QMap:

                  well an associative container then :)

                  You mean a sorted vector in that case? :)

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  2
                  • D Offline
                    D Offline
                    Deedss
                    wrote on last edited by
                    #9

                    I started looking at the QSortFilter and decided to try something with that. In the end I added an extra role to my model which is filled with the propertyIndex of the QProperty. And then I sort based on that index value.

                    1 Reply Last reply
                    2

                    • Login

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