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. How to set QPixmap to QTextBrowser background ?
QtWS25 Last Chance

How to set QPixmap to QTextBrowser background ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.3k 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    How to set QPixmap to QTextBrowser background ?
    The code below has no effect.
    Project: https://github.com/sonichy/Qt_QQMusic

    QPalette palette;
    palette.setBrush(ui->textBrowser->backgroundRole(),QBrush(pixmap_cover));
    ui->textBrowser->setAutoFillBackground(true);
    ui->textBrowser->setPalette(palette);
    

    https://github.com/sonichy

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

      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);
      
      1 Reply Last reply
      1
      • sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by
        #3

        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);");
        

        https://github.com/sonichy

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

          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.

          1 Reply Last reply
          1

          • Login

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