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 get 2 layers using Composition Modes in QPainter?
Forum Updated to NodeBB v4.3 + New Features

How to get 2 layers using Composition Modes in QPainter?

Scheduled Pinned Locked Moved Solved General and Desktop
1 Posts 1 Posters 617 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.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by qwe3
    #1

    Hi,

    I would like to draw ( using QPainter and composition modes ) on the one widget 2 layers:

    1. one, which is top layer - red color
    2. one, which is bottom layer - blue color with black text in some position

    I draw "transparent" ellipse, so I see blue layer.

    I can do 2 layers: red and blue, transparent ellipse using only composition modes, but I can't get text to blue layer. There is not shown.

    goal.png

    My code:

    void someWidget::paintEvent(QPaintEvent *)
    {
        QPainter painter(this);
        painter.setRenderHints(QPainter::Antialiasing);
        painter.setPen(Qt::NoPen);
    
        QPointF globalpoint = this->mapFromGlobal(QCursor::pos());
    
        painter.setCompositionMode(QPainter::CompositionMode_Source);
        painter.fillRect(0, 0, width(), height(), Qt::red);
    
        painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
        painter.setBrush(QColor(0,0,0,0));
        painter.drawEllipse(globalpoint, 200,200);
    
        painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
        painter.fillRect(0, 0, width(), height(), Qt::blue);
    
        painter.setPen(Qt::black);
        painter.drawText(100, 100, "Text");
    }
    

    EDIT:

    I found solution:
    I have to change an order:

        painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
        painter.setPen(Qt::black);
        painter.drawText(100, 100, "Text");
        painter.fillRect(0, 0, width(), height(), Qt::blue);
    
    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