c++ - 存在 StyleSheet 时如何使用 Qt::BackgroundRole 更改背景颜色
问题描述
我试图通过覆盖 Qt::BackgroundRole 的数据方法来设置 QTableView 项的背景颜色。
这在没有 StyleSheet 的情况下有效。
但是对于我设置 QTableView::item 边框属性的 qss,它不起作用。
我需要一些带有通用 StyleSheet 的 QTableView 和一个通过 Qt::DisplayRole 更改背景颜色的 QTableView。
我试图将我的 QTableView::item 分开
setObjectName
:QTableView#SecondView::item。我还尝试通过以下方式从 QTableView 中删除 StyleSheet:
m_p_table_view2->setStyleSheet("");
或者
m_p_table_view2->setStyleSheet(0);
或在样式表中
border: 0;
但没有成功。
MyProxyModel 继承自 QStandardItemModel
class MyProxyModel : public QStandardItemModel
{
public:
MyProxyModel(int in_row, int in_col);
QVariant data(const QModelIndex &in_index, int in_role) const override;
};
然后我覆盖数据方法
QVariant MyProxyModel::data(const QModelIndex &in_index, int in_role) const
{
if(Qt::BackgroundRole == in_role)
{
int tmp = QStandardItemModel::data(in_index, Qt::DisplayRole).toInt();
switch (tmp)
{
case 1:
return QVariant(QColor(0xBB, 0xFF, 0x99));
break;
...
default:
break;
}
}
return QStandardItemModel::data(in_index, in_role);
}
它可以在没有样式表的情况下工作。
但是使用 StyleSheet 它不起作用。
/*------------------------------------------*/
/*default*/
QTableView
{
border: 1px solid #cccccc;
background: #f3f3f3;
color: #252424;
font-family: "Segoe UI";
outline: 0;
}
QTableView::item
{
/* border-bottom: 1px solid #cccccc; */ /* causes the bug */
}
/*------------------------------------------*/
/*second view*/
QTableView#SecondView
{
background: #99dd99;
border: 1px solid #cccccc;
color: #252424;
font-family: "Segoe UI";
outline: 0;
}
QTableView#SecondView::item
{
/* border: 0; */ /* causes the bug too */
}
解决方案
推荐阅读
- github - GitHub - 从模板存储库中提取更改
- c++ - 在 C++ 中向多态树添加功能
- qt - 组件完成时,QML Map 未提供任何坐标
- asp.net-core - ASP.NET Core 和 Angular 7 上的电子邮件确认(使用 IdentityUser)
- nativescript - 如何添加canActivate:[AuthGuard],在同一路由的loadChildren中
- spring-webflux - Spring webflux错误处理程序:如何在错误处理程序中获取请求的反应器上下文?
- typescript - 为什么任何导入都修复了 --isolatedModules 错误?
- excel - VBA匹配原理
- google-cloud-platform - 不知道如何将数据加载到 GCP 笔记本(Platform AI)
- ios - 表格视图单元格呈现不正确