Selected QLabel?
-
Hello
I have a gui with several
QLabels
with the sameQPixamp
on it. However, one of them (the first one) appears like this:
You can see how the first one is "selected" or something while the second one is not. When I look into tab order in the Qt Designer, the labels have no tab order (as I would expect) and the only line that interacts with theQLabel
widget in the entire code is:QIcon ok_icon(":/ok_button.png"); QIcon no_ok_icon(":/cancell_button.png"); ui->label1->setPixmap(myCondition()? ok_icon.pixmap(ui->survey_info_label->size()) : no_ok_icon.pixmap(ui->survey_info_label->size())); ui->label2->setPixmap(myCondition()? ok_icon.pixmap(ui->survey_info_label->size()) : no_ok_icon.pixmap(ui->survey_info_label->size()));
Be aware that
myCondition()
is a fully functional function defined and implemented in another part of the code.Any idea on why that first
QLabel
appears different than the others? I have not been able to reproduce this issue with a small example, sorry. If you can tell me what this style change is in normal conditions I will be able to either change it or revert it.Thank you very much in advance.
-
@apalomer said in Selected QLabel?:
You have to imagine that label1 lives in two different UI files that end up composed in the same GUI
Then you need to use the UI file/class somewhere in the selector to distinguish. (To be clear: you can use your own class names in the selector, not just Qt inbuilt ones like
QFrame
orQLabel
.)Anyway, I have tried the descendant selector but I would like to have the name of the ascendant. Something like this:
The available facilities are in the reference I gave, and that does not include "ascendant" direction, only "descendent", as per CSS too.
I guess I will have to change the names at the end....
I don't see why. Presumably the different
.ui
's classes are different, and that should be usable in the selector.There is also a supplementary way of distinguishing your labels which may suit. In that same reference page see
Property Selector QPushButton[flat="false"]
and https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-using-dynamic-properties. You can add your own dynamic properties even in Designer on a widget, there is some "add property plus-button" on the properties on any widget.
-
since you are working in designer, make sure the properties of label1 and label2 are the same. Also move your size() check outside of the ternary assignments, and into a temporary.
-
I did check and both labels have the same properties in the designer. I even replaced the one with the non desired behaviour with a copy of the one that has the good behavior just in case I miss some parameters (obviously changing the hame to its original name). I also changed the code to:
auto check = myCondition(); auto sz = ui->label1->size(); QPixmap px; if (check) px = ok_icon.pixmap(sz); else px = no_ok_icon.pixmap(sz); ui->label1->setPixmap(px); ui->label2->setPixmap(px);
With the same result.
Any other ideas? -
I have been able to track down the error. I have a style sheet that sets the properties of
label1
widget in the following way:QWidget#label1 { background-color: #bebebe; border-radius: 5px; color: black; font-weight: bold; }
The problem is that I have another widget named
label1
in one of the widgets that I include in my main GUI. Is there a way to discriminate the widget's parents or anything in the style sheet? I would not like to change the names of the labels as they are informative as they are now (obviously they are not named label1). -
@apalomer
They do not seem to be very informative if you have two different labels named "whatever-it-is" yet you want a different style on one of them!Is there a way to discriminate the widget's parents or anything in the style sheet?
Yes indeed. You can reference parentage and many other things. (One obvious question is why you address a
QLabel
via aQWidget
rule instead ofQLabel
. However that will not solve if both your "whatever-it-is" are bothQlabel
s.) Anyway, read through https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types, e.g.Descendant Selector QDialog QPushButton Matches all instances of QPushButton that are descendants (children, grandchildren, etc.) of a QDialog. Child Selector QDialog > QPushButton Matches all instances of QPushButton that are direct children of a QDialog.
-
They are informative in each widget. You have to imagine that
label1
lives in two different UI files that end up composed in the same GUI. Anyway, I have tried the descendant selector but I would like to have the name of the ascendant. Something like this:QFrame#frame1 QLabel#label1 { background-color: #bebebe; border-radius: 5px; color: black; font-weight: bold; }
But it did no do the trick. Am I missing something or is it just not possible? I guess I will have to change the names at the end....
-
@apalomer said in Selected QLabel?:
You have to imagine that label1 lives in two different UI files that end up composed in the same GUI
Then you need to use the UI file/class somewhere in the selector to distinguish. (To be clear: you can use your own class names in the selector, not just Qt inbuilt ones like
QFrame
orQLabel
.)Anyway, I have tried the descendant selector but I would like to have the name of the ascendant. Something like this:
The available facilities are in the reference I gave, and that does not include "ascendant" direction, only "descendent", as per CSS too.
I guess I will have to change the names at the end....
I don't see why. Presumably the different
.ui
's classes are different, and that should be usable in the selector.There is also a supplementary way of distinguishing your labels which may suit. In that same reference page see
Property Selector QPushButton[flat="false"]
and https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-using-dynamic-properties. You can add your own dynamic properties even in Designer on a widget, there is some "add property plus-button" on the properties on any widget.