Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can HTML tags be used in QPushButton:setText ?
QtWS25 Last Chance

Can HTML tags be used in QPushButton:setText ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 888 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    edmh
    wrote on last edited by
    #1

    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 )

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      According the documentation, no.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • E Offline
        E Offline
        edmh
        wrote on last edited by
        #3

        Ok thanks. I guess I'll have to use setStylesheet.

        JonBJ 1 Reply Last reply
        0
        • E edmh

          Ok thanks. I guess I'll have to use setStylesheet.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @edmh
          I think some people use a QLabel as a button to get more styling options, rich text and others.

          E 1 Reply Last reply
          0
          • JonBJ JonB

            @edmh
            I think some people use a QLabel as a button to get more styling options, rich text and others.

            E Offline
            E Offline
            edmh
            wrote on last edited by
            #5

            @JonB Good idea. I might try that approach. Thx.

            1 Reply Last reply
            0
            • E Offline
              E Offline
              edmh
              wrote on last edited by
              #6

              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>

              1 Reply Last reply
              0
              • E Offline
                E Offline
                edmh
                wrote on last edited by
                #7

                Moderator - please mark this topic as 'resolved'
                Thanks.

                SGaistS 1 Reply Last reply
                0
                • E edmh

                  Moderator - please mark this topic as 'resolved'
                  Thanks.

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved