请问如何设置QStyledOptionButton的背景颜色?

加盾男爵 2018-07-03 03:15:43
为了显示数据,我使用了QTableView+自定义模型,然后其中一列用户是一个控制开关,用户用来点击控制设备的启动或停止

然后为了实现这一行的功能,我使用了委托,继承自QStyledItemDelegate;并重写了paint()函数

void ChooseWatchObjectDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyleOptionButton btn;
btn.rect = option.rect;
bool fg = index.model()->data(index).toBool();
if(fg == true)
{
btn.palette.setBrush(QPalette::Button,QBrush(QColor(Qt::green)));//无效
btn.text = tr("启动");
}
else
{
btn.palette.setBrush(QPalette::Background,QBrush(QColor(Qt::red)));//同样无效
btn.text = tr("停止");
}
btn.state |= QStyle::State_Enabled;
if(option.widget != NULL)
QApplication::style()->drawControl(QStyle::CE_PushButton,&btn,painter);
else
option.widget->style();
}


这里我希望给这个按钮根据模型数据的不同,给一个不同的背景颜色,但上面的谁知始终没有颜色的变化。。。
...全文
1424 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
小丶强 2021-04-24
  • 打赏
  • 举报
回复
QPalette pal = button_option.palette; button_option.text = QString::fromLocal8Bit("XX"); pal.setColor(QPalette::Button, QColor(255, 79, 79)); pal.setColor(QPalette::ButtonText, QColor(255, 255, 255)); button_option.palette = pal; 这样试试
加盾男爵 2018-07-10
  • 打赏
  • 举报
回复
引用 4 楼 Marcelxx 的回复:
[quote=引用 3 楼 qazaq408 的回复:]
[quote=引用 2 楼 Marcelxx 的回复:]
试试给painter设置颜色?

上面代码里设了painter的颜色。。无效。。[/quote]

painter->这种设置。[/quote]
painter->????
我尝试了
painter->setBrush(QBrush(QColor(Qt::red)));

但么用
Marcelxx 2018-07-09
  • 打赏
  • 举报
回复
引用 3 楼 qazaq408 的回复:
[quote=引用 2 楼 Marcelxx 的回复:]
试试给painter设置颜色?

上面代码里设了painter的颜色。。无效。。[/quote]

painter->这种设置。
加盾男爵 2018-07-09
  • 打赏
  • 举报
回复
引用 2 楼 Marcelxx 的回复:
试试给painter设置颜色?

上面代码里设了painter的颜色。。无效。。
Marcelxx 2018-07-09
  • 打赏
  • 举报
回复
试试给painter设置颜色?
加盾男爵 2018-07-06
  • 打赏
  • 举报
回复
有人会吗?

16,173

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧