Change descendant QLabel properties on the parent pseudo state

I carefully read the documentation and looked through familiar domains such as google and stackoverflow for quite some time. Unfortunately, all this without any solutions. Therefore, I hope that someone here can be my savior.

Situation:

What I use: PyQt4, Python 2.6, Windows 7

I am trying to create a QLabel (using a CSS file that I import into my program). QLabel is in the QListWidgetItem.

So I have a structure:

QListWidget +QListWidgetItem +QLabel 

Thus, according to the documentation, access to QLabel will be as follows. There is no problem.

 QListWidget::item QLabel { background-color:#000 } 

Problem:

However, I would like the QLabel style to be ONLY changed when I hover over the QListWidgetItem. Using the following code, the pseudo-state is simply ignored for some reason. Thus, the background color applies to QLabels, but it does not respect the pseudo-state.

 QListWidget::item:hover QLabel { background-color:#000 } 

This situation persists when I run my application offline.

What is not a solution:

  • Setting QLabel's pseudo-state to hang will not work, because QListWidgetItem is higher than QLabel.

The main question:

Is there a way to change the properties of the QLabel style when and only when the QListWidgetItem freezes and how can this be achieved?

The goal here is to do it ONLY through css (or qss if you want), which is a separate file that is imported into the program.

Sources:

qss documentation: http://doc.qt.digia.com/4.6/stylesheet-syntax.html

+4
source share
1 answer

You cannot do this, at least in Qt 4.7 (not tested Qt5), here is a good explanation: How to set the color of a child QLabels when hovering a parent QFrame with QSS?

Qt css doesn't seem to support pseudo-states for descendant elements, but the original CSS does this: http://codepen.io/anon/pen/olwHs

You must write code to support this style. Use Enter / LeaveEvent to handle the hover, and then set the style in your code using setStyleSheet()

You can also use a dynamic property to do this to leave styles in .ui QLabel[hover="true"] {} and then set and reset this value in the code.

You can also use the dynamic property for the main widget [hover="true"] QLabel {}

0
source

Source: https://habr.com/ru/post/1499641/


All Articles