How to set QPixmap to QTextBrowser background ?
-
How to set QPixmap to QTextBrowser background ?
The code below has no effect.
Project: https://github.com/sonichy/Qt_QQMusicQPalette palette; palette.setBrush(ui->textBrowser->backgroundRole(),QBrush(pixmap_cover)); ui->textBrowser->setAutoFillBackground(true); ui->textBrowser->setPalette(palette);
-
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)); ui->textBrowser->viewport()->setPalette(palette);
-
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.pixmap_cover.save(QDir::currentPath() + "/cover.jpg"); ...... setStyleSheet("background-color:black;"); ui->textBrowser->setStyleSheet("border-image:url(cover.jpg);");
-
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 toui->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.