Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QAbstractListModel list property for item

QAbstractListModel list property for item

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 927 Views
  • 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.
  • alejandro_pnzA Offline
    alejandro_pnzA Offline
    alejandro_pnz
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • alejandro_pnzA Offline
      alejandro_pnzA Offline
      alejandro_pnz
      wrote on last edited by
      #2

      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;
      
      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