Nominate our 2022 Qt Champions!

How to set QPixmap to QTextBrowser background ?

  • How to set QPixmap to QTextBrowser background ?
    The code below has no effect.

    QPalette palette;

  • Moderators

    You need to use viewport's background role, not the widget's.
    Also, although this will work because palettes are inherited, you should probably set the palette on the viewport, not on the widget and you should get the initial palette from the viewport, not an automatic variable:

    QPalette palette = ui->textBrowser->viewport()->palette();
    palette.setBrush(ui->textBrowser->viewport()->backgroundRole(), QBrush(pixmap_cover));

  • There is no need to use viewport, and change to this:

    palette.setBrush(Qt::Base, QBrush(pixmap_cover));

    But effect is still no well.
    I decide to save the pixmap to local disk first, and then use setStyleSheet("border-image") to apply. + "/cover.jpg");

  • Moderators

    If you have the image in resources saving it to file is totally unnecessary and may cause problems (If your app is installed you might not have write access to the install directory).

    The change to Qt::Base is the same as changing it to ui->textBrowser->viewport()->backgroundRole(), because that's what that method returns, except it's a little more bullet proof if you decide to change background role at some point.

    The stylesheet method is ok too, but the palette brush method works as well. If you'd tell us what "is still no well" means in this case (image not showing? showing in wrong place? something else?) we might be able to help.

Log in to reply