Different hover styling on different labels

回眸只為那壹抹淺笑 提交于 2020-07-31 05:50:13

问题


    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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!