Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Inheritance QMap



  • 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?


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    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


  • Moderators

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


  • Lifetime Qt Champion

    @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


  • Lifetime Qt Champion

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


  • Moderators

    @mrjj said in Inheritance QMap:

    well an associative container then :)

    You mean a sorted vector in that case? :)



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


Log in to reply