Important: Please read the Qt Code of Conduct -

HTML inside QPushButton - how to insert images?

  • Hi all,

    I want to create Buttons similar to the ones here:
    ! Dialog)!

    I have found that the following code be used to format text in QPushButton - but I am not able to insert images into HTML code.

    @QPushButton *button = new QPushButton();
    QTextDocument Text;

    Text.setHtml("<h2><font color=white><font face=\"Segoe UI\">Close This App</font></h2>"
                 "<p>More Text</p>");
    QPixmap pixmap(Text.size().width(), Text.size().height());
    pixmap.fill( Qt::transparent );
    QPainter painter( &pixmap );
    Text.drawContents(&painter, pixmap.rect());
    QIcon ButtonIcon(pixmap);

    Could someone please help? TIA!


  • Try to create widget with your images, text and others and do that:
    @void yourWidget::paintEvent(QPaintEvent*)
    QStylePainter painter(this);
    QStyleOptionButton option;


    painter.drawControl(QStyle::CE_PushButton, option);
    I am not sure but it must work

  • [[Doc:QCommandLinkButton]]s provides a look like that you want. The descriptive text is a plaintext and cannot take HTML/images, though.

  • Got it.Thanks.

Log in to reply