如何在QTableView中设置一些特殊行的背景色
How can I set the background color of some special row in QTableView?
我读了一篇旧的文章,但是这对我来说不起作用。
我想设置其第六个参数为真的每一行的背景颜色。我试图在QSqlRelationalDelegate的子类中覆盖Paint方法,但显然它不做任何事情。
MoviesDelegate::MoviesDelegate(QObject *parent)
: QSqlRelationalDelegate(parent)
{ }
void MoviesDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if( index.sibling( index.row(), 6 ).data().toBool() )
{
QStyleOptionViewItemV4 optionViewItem = option;
optionViewItem.backgroundBrush = QBrush( Qt::yellow );
drawDisplay( painter, optionViewItem,
optionViewItem.rect,index.data().toString() );
drawFocus( painter, optionViewItem, optionViewItem.rect);
}
else
QSqlRelationalDelegate::paint(painter, option, index);
}
我该如何修复它?
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// Grab cell value and cast it to boolean
bool boolValue = index.model()->data(index).toBool();
// Paint cell background depending on the bool value
if(boolValue)
painter->fillRect(option.rect, QColor(179, 229, 255));
else
painter->fillRect(option.rect, Qt::red);
// Paint text
QStyledItemDelegate::paint(painter, option, index);
}
相关文章:
- QTableView:endMoveRows在模型中重置水平页眉大小
- 将QIcon添加到QTableView单元格
- 如何在QTableView中排序和更改日期格式
- QTableview Search
- QTableView 拉伸标头,但保持交互性
- qtableView 在订购项目到达时添加和显示它们任意?
- Ncurses C++打印前景色/背景色交换的文本
- 在设置QStylesHeet之后,如何获得Qwidget背景色
- 如何设置 RTF 编辑控件的背景色
- Visual C++按钮的透明背景色
- 更改MFC中CListCtrl整列的背景色
- 使用 QPropertyAnimation 更改 QPushButton 的背景色
- 在 Linux 上使用 ncurses 的背景色
- 在给定前景色和背景色的情况下,我如何计算第三种颜色
- 获取窗口的背景色
- c++ Win32单选按钮背景色
- 尝试使用GetObject();获取默认背景色
- 如何在QTableView中设置一些特殊行的背景色
- 在PDCurses中设置整个窗口的背景色
- 更改编辑控件的背景色的困难