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