Can HTML tags be used in QPushButton:setText ?
-
I know I can style the text in a QLabel using "<span style:color...> Text </span>" when calling setText. But if I do this on a QPushButton, it does not work. The text on my QPushButton object contains the text "<span style...". In other words the html tags are ignored and taken literally by QPushButton.
Question: Can you use basic HTML style tags in QPushButton?. Yes I am aware you can use 'setStyleSheet' but I don't want to have to call setStylesheet if I just need to quickly color the text in a QPushButton. And I don't want to use QPallet or overide paintEvent to do this. I am using Qt5 on Fedora linux ( 35 )
-
According the documentation, no.
-
@edmh
I think some people use aQLabel
as a button to get more styling options, rich text and others. -
Actually this can be done. You can 'embed' a QLabel in the QPushButton
<code>
puushButton->setLayout(new QGridLayout);QLabel* textLabel = new QLabel("<span style='color:#FF3300; font-size:10pt;'>Hello world!</span>");
textLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter); // or center
textLabel->setAttribute(Qt::WA_TransparentForMouseEvents, true);pushButton->layout()->addWidget(textLabel);
</code>
-
@edmh said in Can HTML tags be used in QPushButton:setText ?:
Moderator - please mark this topic as 'resolved'
Thanks.You can do it yourself either using the "Topic Tools" button or the three dotted menu beside the answer you deem correct.