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. How to implement checkstate for QAbstractItemModel or QAbstractListModel

How to implement checkstate for QAbstractItemModel or QAbstractListModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
qabstractlistmo
8 Posts 3 Posters 2.7k 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.
  • M Offline
    M Offline
    milan
    wrote on 8 Oct 2018, 06:37 last edited by
    #1

    Hello, I have success implementing some methods of QAbstractListModel. How can I add checkstate to the listed items in the model. The model is used by QListView.

    #include "intmodel.h"
    
    IntModel::IntModel(int count,  QObject *parent) : QAbstractListModel(parent)
    {
        for (int i=0; i < count; ++i)
            m_values << i+1;
    }
    
    int IntModel::rowCount(const QModelIndex &parent) const
    {
        if (parent.isValid())
            return 0;
        return  m_values.count();
    }
    
    QVariant IntModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        if(role != Qt::DisplayRole && role != Qt::CheckStateRole) {// && role != Qt::EditRole )
            return QVariant();
        }
    
        if((role == Qt::DisplayRole) && (index.column() == 0 && index.row() < m_values.count()))
            return m_values.at( index.row() );
        else
            return QVariant();
    
    }
    
    Qt::ItemFlags IntModel::flags( const QModelIndex &index ) const
    {
        if(!index.isValid())
            return Qt::ItemIsEnabled;
        return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
    }
    
    bool IntModel::setData( const QModelIndex &index, const QVariant &value, int role )
    {
        qDebug() << "IntModel setData: " << role;
        if((role != Qt::EditRole && role != Qt::CheckStateRole) || index.column() != 0 || index.row() >= m_values.count())
            return false;
    
        if(role == Qt::EditRole) {
            if(value.toInt() == m_values.at( index.row()))
                return false;
            m_values[index.row()] = value.toInt();
            emit dataChanged(index, index);
            return true;
        }
        else if (role == Qt::CheckStateRole) {
    		// TO DO
        }
    }
    
    
    #ifndef INTMODEL_H
    #define INTMODEL_H
    
    #include <QAbstractListModel>
    #include <QDebug>
    
    class IntModel : public QAbstractListModel
    {
        Q_OBJECT
    
    public:
        explicit IntModel(int count, QObject *parent = nullptr);
    
        // Basic functionality:
        int rowCount(const QModelIndex &parent = QModelIndex()) const override;
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    
        Qt::ItemFlags flags(const QModelIndex &index) const override;
        bool setData(const QModelIndex &index, const QVariant &value, int role) override;
    private:
        QList<int> m_values;
    };
    
    #endif // INTMODEL_H
    
    
    R 1 Reply Last reply 8 Oct 2018, 06:42
    0
    • M milan
      8 Oct 2018, 06:37

      Hello, I have success implementing some methods of QAbstractListModel. How can I add checkstate to the listed items in the model. The model is used by QListView.

      #include "intmodel.h"
      
      IntModel::IntModel(int count,  QObject *parent) : QAbstractListModel(parent)
      {
          for (int i=0; i < count; ++i)
              m_values << i+1;
      }
      
      int IntModel::rowCount(const QModelIndex &parent) const
      {
          if (parent.isValid())
              return 0;
          return  m_values.count();
      }
      
      QVariant IntModel::data(const QModelIndex &index, int role) const
      {
          if (!index.isValid())
              return QVariant();
      
          if(role != Qt::DisplayRole && role != Qt::CheckStateRole) {// && role != Qt::EditRole )
              return QVariant();
          }
      
          if((role == Qt::DisplayRole) && (index.column() == 0 && index.row() < m_values.count()))
              return m_values.at( index.row() );
          else
              return QVariant();
      
      }
      
      Qt::ItemFlags IntModel::flags( const QModelIndex &index ) const
      {
          if(!index.isValid())
              return Qt::ItemIsEnabled;
          return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
      }
      
      bool IntModel::setData( const QModelIndex &index, const QVariant &value, int role )
      {
          qDebug() << "IntModel setData: " << role;
          if((role != Qt::EditRole && role != Qt::CheckStateRole) || index.column() != 0 || index.row() >= m_values.count())
              return false;
      
          if(role == Qt::EditRole) {
              if(value.toInt() == m_values.at( index.row()))
                  return false;
              m_values[index.row()] = value.toInt();
              emit dataChanged(index, index);
              return true;
          }
          else if (role == Qt::CheckStateRole) {
      		// TO DO
          }
      }
      
      
      #ifndef INTMODEL_H
      #define INTMODEL_H
      
      #include <QAbstractListModel>
      #include <QDebug>
      
      class IntModel : public QAbstractListModel
      {
          Q_OBJECT
      
      public:
          explicit IntModel(int count, QObject *parent = nullptr);
      
          // Basic functionality:
          int rowCount(const QModelIndex &parent = QModelIndex()) const override;
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
      
          Qt::ItemFlags flags(const QModelIndex &index) const override;
          bool setData(const QModelIndex &index, const QVariant &value, int role) override;
      private:
          QList<int> m_values;
      };
      
      #endif // INTMODEL_H
      
      
      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 8 Oct 2018, 06:42 last edited by
      #2

      @milan
      here is an example: http://programmingexamples.net/wiki/Qt/ModelView/AbstractTableModelCheckable

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      M 1 Reply Last reply 8 Oct 2018, 06:48
      2
      • R raven-worx
        8 Oct 2018, 06:42

        @milan
        here is an example: http://programmingexamples.net/wiki/Qt/ModelView/AbstractTableModelCheckable

        M Offline
        M Offline
        milan
        wrote on 8 Oct 2018, 06:48 last edited by milan 10 Aug 2018, 06:53
        #3

        @raven-worx . Thanks for your response. Yes I have seen this example. I would like to implement it without the need for Item class? But I do not know if it is possible?

        R 1 Reply Last reply 8 Oct 2018, 07:01
        0
        • M milan
          8 Oct 2018, 06:48

          @raven-worx . Thanks for your response. Yes I have seen this example. I would like to implement it without the need for Item class? But I do not know if it is possible?

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 8 Oct 2018, 07:01 last edited by
          #4

          @milan
          well you need to store the checkstate along with your model data/entries. There is no way around.

          For example instead of a QList<int> you could use a QList<QPair<int,Qt::CheckState> > for example, or any other custom struct (instead of QPair)

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          M 1 Reply Last reply 8 Oct 2018, 07:11
          3
          • R raven-worx
            8 Oct 2018, 07:01

            @milan
            well you need to store the checkstate along with your model data/entries. There is no way around.

            For example instead of a QList<int> you could use a QList<QPair<int,Qt::CheckState> > for example, or any other custom struct (instead of QPair)

            M Offline
            M Offline
            milan
            wrote on 8 Oct 2018, 07:11 last edited by
            #5

            @raven-worx . Thanks. Is it possible to use QList<QStandardItem> instead? or QList<QVariant> or neither?

            R 1 Reply Last reply 8 Oct 2018, 07:22
            0
            • M milan
              8 Oct 2018, 07:11

              @raven-worx . Thanks. Is it possible to use QList<QStandardItem> instead? or QList<QVariant> or neither?

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 8 Oct 2018, 07:22 last edited by
              #6

              @milan said in How to implement checkstate for QAbstractItemModel or QAbstractListModel:

              Is it possible to use QList<QStandardItem> instead?

              doesn't make much sense.

              @milan:

              or QList<QVariant> or neither?

              since a QVariant can hold any type of data yes, if you like so.
              But still you will need to create a custom struct, register it's metatype and put it into a QVariant.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              M 1 Reply Last reply 8 Oct 2018, 09:59
              2
              • R raven-worx
                8 Oct 2018, 07:22

                @milan said in How to implement checkstate for QAbstractItemModel or QAbstractListModel:

                Is it possible to use QList<QStandardItem> instead?

                doesn't make much sense.

                @milan:

                or QList<QVariant> or neither?

                since a QVariant can hold any type of data yes, if you like so.
                But still you will need to create a custom struct, register it's metatype and put it into a QVariant.

                M Offline
                M Offline
                milan
                wrote on 8 Oct 2018, 09:59 last edited by
                #7

                @raven-worx. I tried with example you mentioned at http://programmingexamples.net/wiki/Qt/ModelView/AbstractTableModelCheckable. But I found, I cannot hide the checkboxes (if needed). Maybe I plan to use this model for other different views too. Maybe another Listview without checkboxes.

                V 1 Reply Last reply 8 Oct 2018, 10:10
                0
                • M milan
                  8 Oct 2018, 09:59

                  @raven-worx. I tried with example you mentioned at http://programmingexamples.net/wiki/Qt/ModelView/AbstractTableModelCheckable. But I found, I cannot hide the checkboxes (if needed). Maybe I plan to use this model for other different views too. Maybe another Listview without checkboxes.

                  V Offline
                  V Offline
                  VRonin
                  wrote on 8 Oct 2018, 10:10 last edited by VRonin 10 Aug 2018, 10:10
                  #8

                  @milan said in How to implement checkstate for QAbstractItemModel or QAbstractListModel:

                  I cannot hide the checkboxes

                  if you return QVariant() for the CheckStateRole the checkbox will not appear in the default delegate (QStyledItemDelegate)

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  1

                  5/8

                  8 Oct 2018, 07:11

                  • Login

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