Do you have any example code that I could refer. I think the second method will not have a checkbox centered. I tried the following and it creates a normal checkbox, which is not centered. And here again I cannot check or uncheck. I am so confused.
QVariant CheckBoxSqlQueryModel::data( const QModelIndex &index, int role /*= Qt:isplayRole*/ ) const
{
QVariant value = QSqlQueryModel::data(index,role);
if (role == Qt::CheckStateRole && index.column() == 0)
{
return (QSqlQueryModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked;
}
return value;
}
Qt::ItemFlags CheckBoxSqlQueryModel::flags( const QModelIndex &index ) const
{
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if (index.column() == 0)
{
flags |= Qt::ItemIsUserCheckable;
flags |= Qt::ItemIsSelectable;
}
else
{
flags |= Qt::ItemIsEditable;
flags |= Qt::ItemIsSelectable;
}
return flags;
}
bool CheckBoxSqlQueryModel::setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole )
{
if (index.column() == 0)
{
role = Qt::CheckStateRole;
}
QSqlQueryModel::setData(index,value);
return true;
}
Bookmarks