优化QTableView\QTreeView\QListView丢失焦点时item的显示样式

This commit is contained in:
duanshengchao 2025-06-13 10:04:54 +08:00
parent a2fc55ced7
commit b216828f1f
3 changed files with 17 additions and 0 deletions

View File

@ -150,6 +150,11 @@ void AttributeTableDelegate::paint(QPainter* painter, const QStyleOptionViewItem
opt.palette.setColor(QPalette::Text, Qt::red); opt.palette.setColor(QPalette::Text, Qt::red);
opt.palette.setColor(QPalette::HighlightedText, Qt::red); opt.palette.setColor(QPalette::HighlightedText, Qt::red);
} }
else
{
//确保失去焦点时高亮颜色因为上面对于更改的内容会有特殊处理所以不能放在qss样式中统一处理qss样式会优先这里被执行
opt.palette.setColor(QPalette::HighlightedText, QColor(240, 240, 240));
}
//处理被选择时的背景色注意如果qss中有设置会覆盖此处逻辑 //处理被选择时的背景色注意如果qss中有设置会覆盖此处逻辑
if(option.state.testFlag(QStyle::State_Selected)) if(option.state.testFlag(QStyle::State_Selected))

View File

@ -66,6 +66,10 @@ QTableView::item:hover
QTableView::item:selected QTableView::item:selected
{ {
background-color: rgb(49, 91, 125); background-color: rgb(49, 91, 125);
}
QTableView::item:selected:!active
{
color: rgb(240, 240, 240);
}</string> }</string>
</property> </property>
<property name="verticalScrollBarPolicy"> <property name="verticalScrollBarPolicy">

View File

@ -231,6 +231,10 @@ QTreeView::item:selected
{ {
background-color: rgb(49, 91, 125); background-color: rgb(49, 91, 125);
} }
QTreeView::item:selected:!active
{
color: rgb(240, 240, 240);
}
QTreeView::branch:has-children:!has-siblings:closed, QTreeView::branch:has-children:!has-siblings:closed,
QTreeView::branch:closed:has-children:has-siblings QTreeView::branch:closed:has-children:has-siblings
{ {
@ -262,6 +266,10 @@ QListView::item:selected
{ {
background-color: rgb(49, 91, 125); background-color: rgb(49, 91, 125);
} }
QListView::item:selected:!active
{
color: rgb(240, 240, 240);
}
</string> </string>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">