我正在尝试获取父背景并将其设置为复选框背景颜色。我有一个paint方法,它删除了复选框:
void paint( QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex &index) const
{
// Get item data
bool value = index.data(Qt::UserRole).toBool();
QString text = index.data(Qt::DisplayRole).toString();
// Fill style options with item data
const QStyle *style = QApplication::style();
QStyleOptionButton opt;
opt.state |= value ? QStyle::State_On : QStyle::State_Off;
opt.state |= QStyle::State_Enabled;
opt.text = text;
opt.rect = option.rect;
// Draw item data as CheckBox
style->drawControl(QStyle::CE_CheckBox,&opt,painter);
}但是我如何设置这个复选框的背景颜色呢?
发布于 2021-08-18 06:19:48
painter->fillRect(rect, color);发布于 2012-02-10 20:36:09
更新:
相反,您可以尝试从小部件初始化QStyleOptionButton,这样您将获得小部件的调色板。
QStyleOptionButton opt;
opt.initFrom(this);的老建议:
更改opt.backgroundBrush可能会起作用:
opt.backgroundBrush = QBrush(QColor(0, 0, 0, 102));或设置opt.backgroundColor
https://stackoverflow.com/questions/9227730
复制相似问题