Search code examples
pythonpyqtpyqt5qtstylesheets

How to change the font-size of QLabel When hovering In Pyqt5?


Use the following stylesheet code for Custom Label. In hover, background-colour: green; and color-white; works perfectly, but My problem : the font-size:27px; and font-weight: 700; will not work as desired. (No change,in font size and as well as font-weight). How to resolve it?

MyLabel[button63="1"]
{
text-align:left; font-size:17px;  font-family: Calibri;
color:black; background-color:rgb(212,185,150); border-style: flat;
}
MyLabel[button63="1"]:hover
{
font-size: 27px; font-weight:700; background-color: green; color: white; 

}

First Program

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSignal,Qt
import assist_stylesheet_002


class MyLabel(QLabel):
    leftclicked = pyqtSignal()

    def mousePressEvent(self, ev):
        if ev.button() == Qt.LeftButton:
            self.leftclicked.emit()
        QLabel.mousePressEvent(self, ev)
#-----------------------------------------------------------------------------

class AssistMain(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle(" Css Style Sheet")
        self.setGeometry(100,100,600,600)

        self.lbl_country = MyLabel("Country")
        self.lbl_country.setProperty("type",'1')
        self.lbl_country.setFixedSize(100,30)
        self.lbl_state = MyLabel("State")
        self.lbl_state.setProperty("type",'1')
        self.lbl_state.setFixedSize(100,30)
        self.lbl_town = MyLabel("Town")
        self.lbl_town.setProperty("type",'1')
        self.lbl_town.setFixedSize(100,30)

        self.vbox = QVBoxLayout()
        self.vbox.setSpacing(10)
        self.vbox.addWidget(self.lbl_country)
        self.vbox.addWidget(self.lbl_state)
        self.vbox.addWidget(self.lbl_town)
        self.vbox.addStretch()
        # self.setLayout(self.vbox)

        widget = QWidget()
        widget.setLayout(self.vbox)
        self.setCentralWidget(widget)

def main():
    app = QApplication(sys.argv)
    mainwindow = AssistMain()
    qApp.setStyleSheet(assist_stylesheet_002.style_toplayout())
    app.setStyle("fusion")
    mainwindow.show()
    sys.exit(app.exec_())
if __name__ == "__main__":
    main()

CSS Style Sheet

 def style_toplayout():
    return """
    MyLabel[type="1"]
    {
    text-align:left; font-size:17px;  font-family: Calibri;
    color:black; background-color:rgb(212,185,150); border-style: flat;
    }
    MyLabel[type="1"]:hover
    {
    font-size: 27px; font-weight:700; background-color: green; color: white;  
    }
    
    """

Solution

  • It seems that the font cannot be changed dynamically with QSS so a possible solution is to override the enterEvent and leaveEvent methods to change the font using QFont:

    class MyLabel(QLabel):
        leftclicked = pyqtSignal()
    
        def mousePressEvent(self, ev):
            if ev.button() == Qt.LeftButton:
                self.leftclicked.emit()
            QLabel.mousePressEvent(self, ev)
    
        def enterEvent(self, event):
            super().enterEvent(event)
            font = QFont("Calibri")
            font.setPointSize(27)
            self.setFont(font)
    
        def leaveEvent(self, event):
            super().leaveEvent(event)
            font = QFont("Calibri")
            font.setPointSize(17)
            self.setFont(font)