python tablewidget 颜色_更改QTableWidget的默认选择颜色,并使其半透明

我有几乎相同的场景,但在instead中,我在单元格中有文本,我想要完全透明的选择(所以不改变背景颜色)

如果设置透明颜色,它将是实心的(qt中的bug?)所以我将文本设置为粗体(=selected)并转换选择样式

这里的代码,也许会有帮助//.h

#include

class SelectionControlDelegate : public QStyledItemDelegate

{

public:

SelectionControlDelegate(QObject* parent = 0);

void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const override;

};

//.cpp

SelectionControlDelegate::SelectionControlDelegate(QObject* parent) : QStyledItemDelegate(parent)

{

}

void SelectionControlDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const

{

QStyledItemDelegate::initStyleOption(option, index);

const bool selected = option->state & QStyle::State_Selected;

option->font.setBold(selected); // this will represent selected state

if (selected)

{

option->state = option->state & ~QStyle::State_Selected; // this will block selection-style = no highlight

}

}

// in widget class

...

_ui->tableView->setItemDelegate(new SelectionControlDelegate(this));

...

// when setting cell background, i would change also text color

QColor textColor = backgroundColor.value() <= 120 ? Qt::white : Qt::black; // if it is dark, text would be white otherwise black

// or you can compute invert color...

这是我的设想:5%和25%的项目被选中

选择展示

{1美元^

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值