问题
self.scrollArea.setStyleSheet("background-color: rgba(255, 0, 0, 0); border-style:none;\n"
"}\n"
"QLabel#label2 { \n"
" background-color: rgba(0, 0, 0, 120); color: white; text: center; border-style:solid; border-
width: 1px; border-top-style: none;\n"
"}\n"
"QLabel#label3 { \n"
"background-color: rgba(0, 0, 0, 0); color: rgba(255, 255, 255, 0); text: center;\n"
"}\n"
"QLabel:hover + QLabel#label2{ \n"
"background-color: rgb(0, 0, 0, 120); color: rgba(255,255,255,255); text: center;\n"
"}\n"
"QLabel:hover + QLabel#label3 { \n"
"background-color: rgb(118, 185, 0); color: rgba(255,255,255,255); text: center;\n"
"}\n")
I have two labels (label 1 and 2) and I want them both to be styled differently when any label is hovered over. How can I achieve this? I am using css styling in pyqt5. PS: Both my labels are within my scroll area so changes made in the stylesheet do take effect when run.
回答1:
You can set accessible name to each of your label: label.setAccessibleName("label1")
and target it in the stylesheet like this:
QLabel[accessibleName="label1"]{ border: 1px solid red;}
You can also set stylesheet for specific a widget like this:
widget.setStyleSheet('QLabel:hover{border:1px solid red;}')
Also, I would suggest using triple quotes for multiline strings:
stylesheet="""QLabel{
border:1px solid red;
} """
来源:https://stackoverflow.com/questions/63059355/different-hover-styling-on-different-labels