0
点赞
收藏
分享

微信扫一扫

二十一、继承QStyledItemDelegate单元格选定没有背景色的解决办法

zibianqu 06-21 20:40 阅读 4
#ifndef DD_H
#define DD_H

#include <QStyledItemDelegate>

class DD : public QStyledItemDelegate
{
public:
DD();

// QAbstractItemDelegate interface
public:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
};

#endif // DD_H
void DD::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QTextOption op;
op.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QFont font;
font.setFamily("Microsoft YaHei");
font.setPixelSize(14);
painter->setFont(font);
//判断当前 item 是否选中
if (option.state & QStyle::State_Selected) {
//当前 item 被选中,高亮
// painter->fillRect(option.rect, option.palette.highlight());
//自定义选中背景色
painter->fillRect(option.rect, QBrush(Qt::gray));
}

painter->drawText(option.rect, index.data(Qt::DisplayRole).toString(), op);
}


举报

相关推荐

0 条评论