Checkable box with QAbstractTableModel implementation



  • I need to add a checkbox to my TableView through my model. I've already added the

    if ( role == Qt::CheckStateRole && index.column() == CHECKBOX_COL )
    {
        retval = Qt::Unchecked;
    }
    

    I've also attempted the setDataFunction. However, I cannot check the checkbox. I was wondering if anyone had an example where you can check rows of data in a TableView and where I'm able to capture the check or uncheck via a signal. I'll also be implementing check all and uncheck all functionality via buttons; however, would like to get the check buttons working properly first.

    Thanks!


  • Moderators

    @HunterMetcalfe
    did you forget to add the Qt::ItemIsUserCheckable in the model's flag() implementation? (For the relevant model index)



  • @HunterMetcalfe said in Checkable box with QAbstractTableModel implementation:

    where I'm able to capture the check or uncheck via a signal

    If you are implementing your own model add the optional const QVector<int> &roles as last argument to the dataChanged() signal and in the slot check that roles.contains(Qt::CheckStateRole)

    Otherwise.

    https://forum.qt.io/topic/80337/checkable-treeview-not-support-for-check-and-uncheck/4

    replace

    const int numRows = model->rowCount(idx);
    for(int i=0;i<numRows ;++i)
    model->setData(model->index(i,idx.column(),idx),newCheck,Qt::CheckStateRole);
    

    with emit checkChanged(idx);



  • Thanks for the replies! I will try exactly this. I apologize for the delay. Many other projects to be completed.


Log in to reply
 

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