I want to implement a User editable checkbox in QTableView
which is created using QAbstractModel. I am able to assign a checked and unchecked Checkbox but unable to make it editable.
flag is set to QItemIsUserCheckable
.
You can do it easily by implementing model's setData()
method like this:
bool yourModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
if (role == Qt::CheckStateRole)
{
if ((Qt::CheckState)value.toInt() == Qt::Checked)
{
//user has checked item
return true;
}
else
{
//user has unchecked item
return true;
}
}
return false;
}
And don't forget about your model's data()
method:
QVariant ProxyModelSubobjects::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::CheckStateRole && index.column() == COLUMN_WITH_CHECKBOX)
{
//return Qt::Checked or Qt::Unchecked here
}
//...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With