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

Making a generic List



  • Hello, I'm trying to make a generic list with the goal to have some unified functionalities like clear/append etc.

    I'm trying the approach with having a QList<QVariant*>

    I have been trying a lot of stuff, templates for starters. Just to find out that it's not supported in Qt when using QObject.

    Can anyone tell me if it's even possible to do it, or give me some references, that would be amazing.

    Here is my baseList idea for anyone wondering.

    #ifndef BASELIST_H
    #define BASELIST_H
    
    
    class BaseList : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QList<QVariant*> list READ list NOTIFY listChanged)
    
    public:
        BaseList(QObject *parent = nullptr);
    
        QList<QVariant*> list() const;
        QList<QVariant*>* listPointer() const;
    
        void clear();
        void append(QVariant* object);
    
    protected:
        QList<QVariant*>* m_list;
    
    Q_SIGNALS:
        void listChanged();
    };
    
    #endif // BASELIST_H
    
    #include "BaseList.h"
    
    BaseList::BaseList(QObject *parent) :
        QObject(parent),
        m_list(new QList<QVariant*>())
    {
    
    }
    
    QList<QVariant*> BaseList::list() const
    {
        return *m_list;
    }
    
    QList<QVariant*> *BaseList::listPointer() const
    {
        return m_list;
    }
    
    void BaseList::clear()
    {
        qDeleteAll(*m_list);
        m_list->clear();
        Q_EMIT listChanged();
    }
    
    void BaseList::append(QVariant* object)
    {
        m_list->append(object);
    }
    
    




  • @artwaw
    Well, yes and no...
    The idea is to have a class that inherits this class and automatically has the exposed Q_PROPERTY and additional stuff though inheritance.

    So for example I would make a class ChildList : public BaseList
    And this ChildList should use the QList<QVariant*> to store a custom class -> QList<CustomClass*>

    #ifndef CHILDLIST_H
    #define CHILDLIST_H
    
    #include "BaseList.h"
    
    class ChildList : public BaseList
    {
        Q_OBJECT
    public:
        ChildList();
        int summarize();
        void populate();
    };
    
    #endif // CHILDLIST_H
    
    #include "ChildList.h"
    #include "CustomClass.h"
    
    ChildList::ChildList() : BaseList()
    {
    
    }
    
    int ChildList::summarize()
    {
        int totalSum = 0;
        for(int i = 0; i < m_list->count(); i++){
            QVariant* var = m_list->at(i);
            CustomClass* temp = (CustomClass*) var->value<void *>();
            totalSum += temp->getSum();
        }
        return totalSum;
    }
    
    void ChildList::populate()
    {
        for(int i = 0; i < 10; i++){
            CustomClass* temp = new CustomClass(i);
            QVariant* var = new QVariant();
            var->setValue(temp);
            m_list->append(var);
        }
    }
    

    This example doesn't work. But i think it makes my idea a little clearer.


Log in to reply