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. [SOLVED] Rendering QWidget to QImage loses alpha-channel
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Rendering QWidget to QImage loses alpha-channel

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 5.5k 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    I have a simple Qt widget. It's a QLabel with a simple CSS style applied. The important part of the style is a round border:
    @QString css("border-style: solid;"
    "border-width: 3px;"
    "border-radius: 7px;");@

    It is displayed on screen fine. The corners of the label beyond the border are filled with transparent color, so it looks great on any background. Here's how it looks when displayed over another widget (which has dark grey background color):

    !http://i.piccy.info/i9/2ef6908c7fb5142ff245b328f5c52207/1385975408/19127/651013/Untitled_2.png(No problem)!

    Now, when I render it to QImage like so
    @QImage bitmap(label->size(), QImage::Format_ARGB32);
    QPainter painter(&bitmap);
    label->render(&painter);
    bitmap.save("C:/1.png");@

    I get this (image opened in image editor to demonstrate a problem clearly):

    !http://i.piccy.info/i9/d217be7e3973e6b5451080d97ab1470a/1385975440/20597/651013/Untitled_3.png(Problem)!

    Note how transparency is not preserved around the corners. What's the problem? How can I render it correctly?

    P. S. I have tried this to test that QImage is capable of saving alpha channel, and that my image editor displays it correctly:
    @bitmap.fill(QColor::fromRgba(qRgba(0, 0, 0, 0)));
    bitmap.save("C:/1.png");@

    It works fine, I can see transparency as checkered pattern.

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      just fill the image with transparency (like you already did) before you render the widget to the image.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #3

        That doesn't help. the rendered image looks exactly the same. Looks like the corners are actually being painted white.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          ok one more possibility you can try:
          @
          QImage bitmap(label->size(), QImage::Format_ARGB32);
          bitmap.fill(Qt::transparent);
          QPainter painter(&bitmap);
          balloon->render(&painter, QPoint(), QRegion(), QWidget::DrawChildren);
          bitmap.save("C:/1.png");
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Violet Giraffe
            wrote on last edited by
            #5

            That works! Can you explain why, what the difference is?

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              take a look at the default parameters of QWidget::render().
              It draws a window background by default if you don't leave it out intentionally.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              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