CheckBox inside a QTableView
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
Code:
{
if (index.column()==5)
{
if (role==Qt::CheckStateRole)
return false;
}
else
}
{
if (index.column()==5)
{
if (role==Qt::CheckStateRole)
}
else
}
Qt
::ItemFlags MyItemModel
::flags( const QModelIndex & index
) const{
if (index.column()==5)
return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsUserCheckable;
return Qt::ItemIsEditable|Qt::ItemIsEnabled|Qt::ItemIsUserCheckable|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
Re: CheckBox inside a QTableView
You have to use custom delegate.
Re: CheckBox inside a QTableView
Quote:
Originally Posted by
axeljaeger
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