QT 颜色取反

在QT中,有时需要使当前颜色与背景的颜色进行取反,特别是在QGraphics模型中,需要应用到。

如自定义类型的item,需要与背景取反,则需要在paintevent中设置取反颜色和painter属性

painter->setBrush(QBrush(Qt::white));

painter->setCompositionMode(QPainter::CompositionMode_Difference);

 

例:

void DrawRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

    painter->save();
    painter->setPen(Qt::SolidLine);//实线
    painter->setBrush(QBrush(Qt::white));  //白色取反则是黑色
    painter->setCompositionMode(QPainter::CompositionMode_Difference);
    
    painter->drawRect(localRect);

    painter->restore();
}

 运行效果:

QT 颜色取反_第1张图片

 

源码下载(版本qtcrator 5.4.1):

https://download.csdn.net/download/qq_35359581/10705422

你可能感兴趣的:(QT)