PDA

View Full Version : A problem with checkbox in QTableView with delegate and model.



apango
4th April 2013, 17:43
Hi, I encounter a problem in create a checkbox column in QTableView

I create a checkbox only column in QTableView with delegate and model. The checkbox works fine, however the background color of the checkbox is not responsible for selection. When a row selected, the background color of the cells in this row will be dark blue. But the check box cell's background color is still white. I need it to be responsible for selection. It is looked like as the snap picture. The code is also put below.

So, how should I do to make the check box cell show the right background color when selected?

8884

The code of paint() in deletegate:




void CheckBoxDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
bool data = index.model()->data(index, Qt::DisplayRole).toBool();
QStyleOptionButton checkboxstyle;
QRect checkbox_rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxstyle);
checkboxstyle.rect = option.rect;
checkboxstyle.rect.setLeft(option.rect.x() + option.rect.width()/2 - checkbox_rect.width()/2);
checkboxstyle.palette.setColor(QPalette::Highlight , index.data(Qt::BackgroundRole).value<QColor>());
if(data)
checkboxstyle.state = QStyle::State_On|QStyle::State_Enabled;
else
checkboxstyle.state = QStyle::State_Off|QStyle::State_Enabled;
QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkboxstyle, painter);
}



I also reimplement date(), setdata() and flags() in model:


QVariant UniqueCheckRowModel::data( const QModelIndex &index, int role /*= Qt::DisplayRole*/ ) const
{
QVariant value = QSqlRelationalTableModel::data(index,role);


if (role == Qt::CheckStateRole && index.column() == colNumberWithCheckBox)
{
return (QSqlRelationalTableModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked;
}
return value;
}

Qt::ItemFlags UniqueCheckRowModel::flags( const QModelIndex &index ) const
{
Qt::ItemFlags flags = QSqlRelationalTableModel::flags(index);
if (index.column() == colNumberWithCheckBox)
{
flags |= Qt::ItemIsUserCheckable;
flags |= Qt::ItemIsSelectable;
}
else
{
flags |= Qt::ItemIsEditable;
flags |= Qt::ItemIsSelectable;
}
return flags;
}

bool UniqueCheckRowModel::setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole )
{
if (index.column() == colNumberWithCheckBox)
{
role = Qt::CheckStateRole;
}
QSqlRelationalTableModel::setData(index,value);
return true;
}

alizadeh91
5th April 2013, 07:12
You have to reimplement the paint method in delegate and write these:



if(option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect,option.palette.highlight());
}

apango
8th July 2013, 17:17
I find a better way, just use the QItemDelegate::drawBackground(painter,option,index ); function in paint

alizadeh91
13th July 2013, 09:05
It's easier not better! It's not better because the purpose of code in paint method is more obvious and neat than drawBackground() method ;)