我有几乎相同的场景,但在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美元^