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);@ -
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.. -
-
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.
-