2

I made 2 labels and linked them using html tag(<a></a>) and make event for each of them when they are hovered. Problem is that the only working event is background-color and attributes like color , font-size doesn't work

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import *

def first_label () :
    style = """
    QLabel#label:hover{
        color: green;
    }
    """
    label.setStyleSheet(style)

def second_label (link) :
    style = """
    QLabel#label2:hover{
        background-color: green;
    }
    """
    label2.setStyleSheet(style)  


app = QtWidgets.QApplication(sys.argv)

window = QtWidgets.QWidget()
window.resize(472, 379)

# First QLabel---------------------------------------
label = QtWidgets.QLabel(window)
label.setGeometry(QtCore.QRect(180, 120, 111, 41))
font = QtGui.QFont()
font.setPointSize(20)
label.setFont(font)
label.setOpenExternalLinks(True)
label.setObjectName("label")
label.setText("<a style='text-decoration:none; color : black; font-size:100%;' href='#'> First </a>")
label.linkHovered.connect(first_label)
# Second QLabel---------------------------------------
label2 = QtWidgets.QLabel(window)
label2.setGeometry(QtCore.QRect(180, 120, 111, 41))
font = QtGui.QFont()
font.setPointSize(20)
label2.setFont(font)
label2.setOpenExternalLinks(True)
label2.setObjectName("label2")
label2.move(174,160)
label2.setText("<a style='text-decoration:none; color: black; font-size:100%;' href='#'> Second </a>")
label2.linkHovered.connect(second_label)

window.show()
sys.exit(app.exec_())
Sina
  • 19
  • 3
  • Are you just trying to get a hover effect for the whole label, or do you intend to have other text that is *not* an anchor (link)? – musicamante Aug 01 '22 at 17:27
  • @musicamante I'm trying to get a hover effect for the whole label – Sina Aug 01 '22 at 17:42
  • Then don't use links. Just use normal text, and set a stylesheet with proper selectors: `QLabel { color: black; }` `QLabel:hover { color: green; }`. – musicamante Aug 01 '22 at 17:51
  • @musicamante thanks for your suggestion But I need the link – Sina Aug 01 '22 at 17:55
  • For what? In your code those links do absolutely nothing. If the labels won't have any other text than the link, then you can just create a custom label subclass that accepts mouse clicks and emits a signal. – musicamante Aug 01 '22 at 17:59
  • @musicamante Thank you for your answer , but this code is just an example to show the problem . I saw [this answer](https://stackoverflow.com/questions/68690207/how-do-i-change-the-color-of-a-link-temporarily-when-hovered-in-a-qlabel) but I'm looking for simple ways – Sina Aug 01 '22 at 18:10

0 Answers0