现在有一个表格,用于
显示一组设备的状态,其中一列是设备的开关,用户可以点击控制设备的启动/停止

对于这一列,我使用了委托,应为需要
按钮是显示在上面的,所以我使用了委托,
继承自QStyledDelegate
- 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();
- }
这里
没有使用
createEditor(
QWidget *
parent, const QStyleOptionViewItem &
option, const QModelIndex &
index)函数,应为这个函数生产的按钮只有用户双击后才会显示,而我需要向上图那样,按钮直接在表格上
然后我希望表格上的按钮可以有个背景
颜色,但我各种方法都试过了,始终没有改变这个QStyledOptionButton的背景颜色,我想问下有没有改这个背景颜色的办法,最好有代码