QAbstractListModel list property for item



  • Hi all. I've use for QML model custom QAbstractListModel object:

    class TaskModel : public QAbstractListModel
    {
        Q_OBJECT
    public:
        enum TaskRoles {
            IdRole = Qt::UserRole + 1,
            NameRole,
            TypeRole,
            LimitRole,
            StateRole,
            CollectModeRole,
            SuccessedActionsCountRole,
            FailedActionsCountRole,
            DelayFromRole,
            DelayToRole
        };
    
        TaskModel(QObject *parent = 0);
        TaskModel(const TaskModel& model);
        int rowCount(const QModelIndex & parent = QModelIndex()) const;
        QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
        QHash<int, QByteArray> roleNames() const;
    
        class Stats {
        public:
            Stats(uint successActionsCount, uint failedActionsCount):
                successActionsCount_(successActionsCount),
                failedActionsCount_(failedActionsCount){}
    
            uint successActionsCount() const;
            uint failedActionsCount() const;
    
            void setSuccessActionsCount(const uint &successActionsCount);
            void setFailedActionsCount(const uint &failedActionsCount);
    
        private:
            uint successActionsCount_;
            uint failedActionsCount_;
        };
    
        class Task
        {
        public:
            Task(uint id, QString name, QString type,
                 uint limit, uint state,
                 const Stats &stats,
                  uint scheduleTimeInSeconds,
                 uint delayFrom, uint delayTo);
    
            uint id() const;
            QString name() const;
            QString type() const;
            uint limit() const;
            uint state() const;
            uint accountCount() const;
            uint delayFrom() const;
            uint delayTo() const;
    
            void setState(const uint &state);
            void setDelayFrom(const uint &delayFrom);
            void setDelayTo(const uint &delayTo);
    
            Stats stats() const;
            void setStats(const Stats &stats);
    
        private:
            uint id_;
            QString name_;
            QString type_;
            uint limit_;
            uint state_;
            Stats stats_;
            uint delayFrom_;
            uint delayTo_;
        };
        typedef std::shared_ptr<Task> TaskPtr;
    
        void addTask(const TaskPtr &task);
        void removeTask(int id);    
        void updateTask(int id, const Task& task);
    
        QList<TaskPtr> tasks() const;
    
    private:
        QList<TaskPtr> tasks_;
    };
    }
    Q_DECLARE_METATYPE(view::TaskModel)
    

    The model works fine. But now I need to add a new property to the Task item - list of objects (e.g. list of Account).
    I've tried use Q_PROPERTY, QQmlListProperty, QVariant with QList of Account. Two first variants returns undefined in QML-side, in last variant I can't get value of QVariant (console.log of this element of the model returns something like "QVariant(Qlist::account)' - returns undefined.
    How can I set this list property?



  • Now solved:
    Just use QVariantList with QVariantMap as values:

            QVariantList extendedStats;
            QVariantMap userAlreadySent, userAlreadyInGroup;
    
            userAlreadySent.insert("name", "Already sent");
            userAlreadySent.insert("value",  eventStats->userAlreadySent());
    
            userAlreadyInGroup.insert("name", "Already in group");
            userAlreadyInGroup.insert("value", eventStats->userAlreadyInGroup());
    
            extendedStats << userAlreadySent << userAlreadyInGroup;
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.