Search code examples
pyqtpyqt5qtstylesheets

how to change QComboBox text background color when mouse hover it(QSS)?


I need to change QComboBox background to red when mouse hover it;but in my qss style the QComboBox drop-down button change to red and the drop-down look like stranged(need to keep system default), it's look like raised style, that is not i wanted.

The simple qss style is:

QComboBox:hover {
    background: red;
}

It looks like this when i try.


Solution

  • Try it:

    import sys
    from PyQt5 import QtWidgets
    
    class Main(QtWidgets.QWidget):
        def __init__(self):
            super(Main, self).__init__()
    
            sheets = [str(i) for i in range(1, 10)]  
    
            combo = QtWidgets.QComboBox()
            combo.addItems(sheets)                             
    
            layout = QtWidgets.QVBoxLayout()
            layout.addWidget(combo)
            self.setLayout(layout)
    
    
    StyleSheet = """ 
    QComboBox {
        border: 1px solid gray;
        border-radius: 3px;
        padding: 1px 18px 1px 3px;
        min-width: 6em;
    }
    QComboBox:hover {
        background: red;
        color: #fff;
    }
    
    """
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        app.setStyleSheet(StyleSheet)
        main = Main()
        main.show()
        sys.exit(app.exec_())
    

    enter image description here