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. Paint with background color and transparent pixmap in foreground
Forum Update on Monday, May 27th 2025

Paint with background color and transparent pixmap in foreground

Scheduled Pinned Locked Moved General and Desktop
qbrushqpainter
2 Posts 1 Posters 3.5k 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.
  • H Offline
    H Offline
    helgur
    wrote on 22 May 2015, 10:17 last edited by helgur
    #1

    I'm using QPainters fillrect method to paint a rect on a scene (second conditional statement):

    
    void TheSquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)  {
        QRectF rec = boundingRect();
    
        Q_UNUSED(widget);
        Q_UNUSED(option);
    
        QBrush brush;
        QColor bordercolor;
        QPen borderpen;
    
        if (this->isSelected()) {
            // works as planned
            brush = QBrush(Qt::yellow);
            bordercolor = QColor(Qt::black);
            borderpen = QPen(bordercolor,1);
            painter->setBrush(Qt::Dense3Pattern);
        } else if (doOperation || hasChanged) {
            // does not work, here's the problem
            brush = QBrush(color,pix);
            bordercolor = QColor(Qt::black);
            borderpen = QPen(bordercolor,1);
        } else if (!hasChanged) {
            // works as planned
            brush = QBrush(Qt::white);
            bordercolor = QColor(Qt::black);
            borderpen = QPen(bordercolor,1);
        }
    
        painter->setPen(borderpen);
        painter->fillRect(rec, brush);
        painter->drawRect(rec);
    }
    

    Problem is that it is only painting the texture pixmap, not the background color. How would I proceed to fill the rect with a background color and the pixmap in the foreground?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      helgur
      wrote on 23 May 2015, 00:13 last edited by helgur
      #2

      The solution was pretty easy, but I overlooked it:

      QBrush bgBrush,fgBrush;
      
      bgBrush = QBrush(color);
      fgBrush = QBrush(pixMap);
      
      painter->setPen(borderpen);
      painter->fillRect(rec, bgBrush);
      painter->fillRect(rec, fgBrush);
      painter->drawRect(rec);
      

      Just a brainfart from my side, nothing to see here. Move on!

      1 Reply Last reply
      0

      1/2

      22 May 2015, 10:17

      • Login

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