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 add the check box with item, and Checkbox should check by the user.

How to add the check box with item, and Checkbox should check by the user.

Scheduled Pinned Locked Moved Solved General and Desktop
qcheckboxqmodelidexqstandarditemqstandarditemmoqsortfilterprox
5 Posts 3 Posters 2.9k 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.
  • Y Offline
    Y Offline
    Yash001
    wrote on last edited by
    #1
      auto hwList = new QListView();
    
      class HardwareProxyModel : public  QSortFilterProxyModel { 
             // Do some Filtering 
        }
        
        auto hwListProxyModel = new HardwareProxyModel;
        hwListProxyModel->setSourceModel(new QStandardItemModel(0, 0));
        hwList->setModel(hwListProxyModel);
    

    I access model

      auto model = hwListProxyModel->sourceModel();
    

    and Add the item in model

     auto count = model->rowCount();
     model->insertRow(count);
     auto index = model->index(count, 0);
     model->setData(index, it->name, Qt::DisplayRole);
     model->setData(index, it->channelAmount, Qt::UserRole);
    

    I can add the Item with check box with help of model->setData(index, Qt::Unchecked, Qt::CheckStateRole);

    I wanted to add the Item with check box, and that check box should check by user. In above logic, I can add the check box, but user is not able to do check the item.

    What Should I have to do if I want to give access to user for check mark?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You have to make the items user checkable:
      http://doc.qt.io/qt-5/qstandarditem.html#setFlags
      http://doc.qt.io/qt-5/qt.html#ItemFlag-enum

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • Y Offline
        Y Offline
        Yash001
        wrote on last edited by
        #3

        Thank Christian, Yes If I just use the QStandardItemModel then i can set the Flag and Make it user check able but here I am using With QSortFilterProxyModel. that's why, I am not getting setflag property and setItem Property.

        Is It any way for setting flag of the model item, if the model is inherit fromQSortFilterProxyModel?

        mrjjM 1 Reply Last reply
        0
        • Y Yash001

          Thank Christian, Yes If I just use the QStandardItemModel then i can set the Flag and Make it user check able but here I am using With QSortFilterProxyModel. that's why, I am not getting setflag property and setItem Property.

          Is It any way for setting flag of the model item, if the model is inherit fromQSortFilterProxyModel?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Yash001
          Hi
          yes there is also a method
          http://doc.qt.io/qt-5/qabstractitemmodel.html#flags

          1 Reply Last reply
          1
          • Y Offline
            Y Offline
            Yash001
            wrote on last edited by
            #5

            Thank you @mrjj and @Christian for answer. I am able to make user Checkable.

            class CustomListModel : public QStandardItemModel {
            public:
            Qt::ItemFlags CustomListModel::flags(const QModelIndex & index) const {
            Qt::ItemFlags defaultFlags = QStandardItemModel::flags(index);
            if (index.isValid()) {
            return defaultFlags | Qt::ItemIsUserCheckable;
            }
            return defaultFlags;
            }

            CustomListModel(const int row, const int coloumn) :
            	QStandardItemModel(row, coloumn) {
            
            }
            

            };

            and modification.
            hwListProxyModel->setSourceModel(new CustomListModel (0, 0));

            1 Reply Last reply
            1

            • Login

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