Checkbox in Qtableview.



  • i have created a Qtableview using Qabstracttablemodel. i want to add a checkbox to a column which user can operate. i set the flag to usercheckable. but i m not geting how user can interatct using the checkbox. means how to genrate a signal when it is checked/uncecked. and how i can catch the signal.



  • it will works fine by using QTableWidget
    @
    QCheckBox *cb1..

    QTableWidgetItem *item = new QTableWidgetItem();
    item->data(Qt::CheckStateRole);
    item->setCheckState(Qt::Unchecked);
    tabWidget->setCellWidget(i,j,cb1);@



  • thnx for reply.. we can do it by Qtablewidget, but is there any way to the same thing in Qtableview .as in my project iam using the model view.???



  • you can do something like this:
    @
    QStandardItem* item;
    item = new QStandardItem(true);
    item->setCheckable(true);
    item->setCheckState(Qt::Checked);
    model->setItem(x,y, item); //add to QAbstractItemModel to cell at x,y
    @
    to read from it anywhere in program use:
    @
    QModelIndex index;
    index= tableView->model()->index(x,y, QModelIndex());
    if(index.data(Qt::CheckStateRole) == Qt::Checked){
    //do something
    }
    @

    Hope this helps



  • thnx for reply,,,here as per i tried model can be QAbstractItemModel *model = index.model();

    which doesnt have a function setItem ...can u be little more clear in which function i need to initialize my QStandardItem* item; (ie in data or flag).
    and even though my flag is set to QAbstractItemModel::flags(Index) | Qt::ItemIsUserCheckable..



  • It certainly is possible to do with your own models. Just be sure that you reimplement flags() to return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable for your item. Also, data() needs to return a valid value for the Qt::CheckStateRole.



  • thx Andre for reply.i have already tried to reimplement my Flag(),,but i am not getting how to return the valid value in Qt::checkstaterole...
    if i return Qt::unchked or Qt::checked...it is fixed not user editable..
    can u help how can i return valid Qt::checkstaterole.



  • Well, it is obviously up to you to determine which value to return. The checked state must correspond with some value in your underlying data store, I'd assume. So, depending on the state of that store, return either Qt::Unchecked or Qt::Checked.

    Also: don't forget to implement setData() as well. This method will be called whenever the user checks or unchecks the checkbox. Changing the state should result in an emit of the dataChanged() signal. If you don't implement it, the state of the checkbox won't change on clicking it.


Log in to reply
 

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