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. Two colours and center text in QPushButton
Forum Updated to NodeBB v4.3 + New Features

Two colours and center text in QPushButton

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 3.4k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    chavesjm
    wrote on last edited by
    #1

    I need a QPushButton with two colors in the text. I found a solution with a html code in QTextDocument and it's working. But I need center align and the html code isn't working.

    @QTextDocument Text;
    Text.setHtml("<p align=center><font>Button</font><br/><font color=yellow>1</font></p>");

    QPixmap pixmap(Text.size().width(), Text.size().height());
    pixmap.fill( Qt::transparent );
    QPainter painter(&pixmap);
    Text.drawContents(&painter, pixmap.rect());

    QIcon ButtonIcon(pixmap);
    ui->toolButton->setText("");
    ui->toolButton->setIcon(ButtonIcon);
    ui->toolButton->setIconSize(pixmap.rect().size());@

    And I get,
    !http://i.stack.imgur.com/5Gmov.png(Button)!

    The same code HTML is working in a QLabel

    @ui->label->setText("<p align=center><font>Label</font><br/><font color=yellow>1</font></p>");@

    !http://i.stack.imgur.com/PZgvb.png(Label)!

    Any solution?

    Thank very much.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      AFAIK QPushButton or QToolButton doesnot accept RichText so HTML type code will not work.
      The best way in this case would be to subclass QPushButton to create your own button and then override paintevent() to draw the text in whatever way you like.

      157

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Why do you need a QTextDocument or HTML at all?
        You can directly draw the text on the pixmap with any color and alignment you want using "QPainter::drawText":http://qt-project.org/doc/qt-5/qpainter.html#drawText-4 with Qt::AlignCenter as part of the flags argument.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mohmo
          wrote on last edited by
          #4

          you can use painterevent of the push button to draw every thing you want.

          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