PDA

View Full Version : QTableView with User checkable checkbox



hamidarr
2nd June 2011, 17:58
hi everyone.i have a QTableView that the first column must have user editable checkbox i reimplemen the data and flags but user can'nt check or unckeck the checkbox this is my code:
q::q(QObject *parent)
:QAbstractTableModel(parent)
{

}
int q::rowCount(const QModelIndex &parent) const
{
return 3;
}
int q::columnCount(const QModelIndex &parent) const
{
return 3;
}
QVariant q::data(const QModelIndex &index, int role) const
{
if(index.column() == 0 && role == Qt::CheckStateRole)
return Qt::Unchecked;
return QVariant();
}
Qt::ItemFlags q::flags(const QModelIndex &index) const
{

if(index.column() == 0 )
return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
Qt::ItemFlags flags = QAbstractItemModel::flags(index);

}

Santosh Reddy
2nd June 2011, 18:13
You need to write the checked status to the model, this should done in setData() method, check this out..

1. Save the checked status of item in your model (to be done in setData())
2. Send it back to the view when requested (to be done in data())


QVariant q::data(const QModelIndex &index, int role) const
{
if(index.column() == 0 && role == Qt::CheckStateRole)
{
// return the state saved in setData (refer setData())
}
return QVariant();
}

bool q::setData(const QModelIndex& index, const QVariant& value, int role)
{
if(role == Qt::CheckStateRole)
{
// Set the checked status internally in your model
}
emit dataChanged(index,index);
return true;
}

joyer83
2nd June 2011, 18:16
You're always returning Qt::Unchecked within data() method, so the checkbox will always be in unchecked state, no matter what.
You need to provide a way to to modify the checkbox's state to Checked or Unchecked state. That is done through setData() method, which you haven't implemented.
Also, please use CODE-tags around your code, it is hard to read it like it is now.