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. Problem with erasing of rectangle area in paintEvent
Qt 6.11 is out! See what's new in the release blog

Problem with erasing of rectangle area in paintEvent

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 667 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.
  • R Offline
    R Offline
    RazrFalcon
    wrote on last edited by
    #1

    I need to draw two overlapping rects. Both with transparent.
    Some thing like this: http://itmages.ru/image/view/1133740/8fdac9c4
    So we have one rect with transparent under other without transparent, then we group it and set transparent to group (example made in Inkscape).

    I tries to make it in qt, but have some issues when I paint it in widget which over parent.

    First way:
    @ QPainter p(this);
    // widget border
    p.drawRect(0, 0, width()-1, height()-1);
    p.setBrush(Qt::lightGray);
    p.setOpacity(0.3);
    p.drawRect(0, 0, 200, 200);
    p.setOpacity(1);
    p.eraseRect(100, 100, 200, 200);
    p.setOpacity(0.3);
    p.drawRect(100, 100, 200, 200);@

    Result: http://itmages.ru/image/view/1133748/ed8fbadb

    Second way:
    @ QPainter p(this);
    // widget border
    p.drawRect(0, 0, width()-1, height()-1);
    p.setBrush(Qt::lightGray);
    p.setOpacity(0.3);
    p.drawRect(0, 0, 200, 200);
    p.setCompositionMode(QPainter::CompositionMode_Clear);
    p.drawRect(100, 100, 200, 200);
    p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    p.drawRect(100, 100, 200, 200);@

    Result: http://itmages.ru/image/view/1133750/0be2675f

    Example shows that after erasing widget get wrong background color of parent widget. In my case it green, but it draws like default system color.

    Source code: https://dl.dropboxusercontent.com/u/16019144/PaintExample.zip

    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