PDA

View Full Version : CheckBox inside a QTableView



voilier92
19th January 2010, 19:15
Hi,
I try to have a checkBox inside a QtableView. For the moment, I try differents solutions (delegate, ..), but I'am not happy with the result. This is the code, I have for the moment
Column 5 must be a checkBox
class MyItemModel is derivated from QStandardItemModel



bool MyItemModel::setData ( const QModelIndex & index, const QVariant & value, int role)
{
if (index.column()==5)
{
if (role==Qt::CheckStateRole)
return QStandardItemModel::setData(index,value,role);
return false;
}
else
return QStandardItemModel::setData(index,value,role);

}
QVariant MyItemModel::data ( const QModelIndex & index, int role ) const
{
if (index.column()==5)
{
if (role==Qt::CheckStateRole)
return QStandardItemModel::data(index,role);
return QVariant();
}
else
return QStandardItemModel::data(index,role);
}
Qt::ItemFlags MyItemModel::flags( const QModelIndex & index ) const
{
if (index.column()==5)
return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsU serCheckable;
return Qt::ItemIsEditable|Qt::ItemIsEnabled|Qt::ItemIsUse rCheckable|Qt::ItemIsSelectable;
}

When I add a line, I use this call
setData(model->index(0, 5), Qt::Checked,Qt::CheckStateRole);


With that, I have a checkbox, but it's not centered and there is a text zone (I can edit it, if I put the flag Qt::ItemIsEditable). I woul like to remove the edit zone and center the check box.
Thank for your help
Laurent

axeljaeger
19th January 2010, 21:59
You have to use custom delegate.

anujgupta131
5th March 2010, 05:04
You have to use custom delegate.

Hi,

I have tried using custom delegates and it works fine.

But i have another problem. If sorting is enabled in QTableView and i change the sort order then the delegate disappears i.e. it is not used after the model is sorted.

Can some one suggest a way to enable sorting while using custom delegates.

Another approach which i will be trying is to use the QHeaderView signal to indicate that sorting order has been changed and then call QTableView::sortByColumn


thanks,

Anuj