Paint with background color and transparent pixmap in foreground



  • 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?



  • 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.