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
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