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

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



  •   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?


  • Qt Champions 2019



  • 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?


  • Lifetime Qt Champion



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