QTableView 中 选中item如何加粗&高亮显示

在QTableView中,可以通过qss 设置选中的颜色,但没法改变字体,比如:

QTableView::item:selected
{
	color: black;
	font-weight:bold;    // 不好使
}

所以只能通过代理来实现。

我们先把选中的框去掉,去掉选中的框可以用代理,也可以设置一下取消focus状态来完成,比如在QtCreator里,把focus选为NoFocus即可:

QTableView 中 选中item如何加粗&高亮显示_第1张图片

 

 下面我们来设置 选中后 让字体变成粗体:

void AlignCenterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem new_option(option);
    new_option.displayAlignment = Qt::AlignCenter;        // 居中显示

    if (new_option.state & QStyle::State_Selected) {
        new_option.font.setBold(true);                    // 变为粗体
    }

    QStyledItemDelegate::paint(painter, new_option, index);
}

如果希望修改选中时字体的颜色,可以用qss设置

QTableView::item:selected {
	color: rgb(16, 36, 60);
}

效果:

QTableView 中 选中item如何加粗&高亮显示_第2张图片

你可能感兴趣的:(qt)