您可以使用QPainter
的compositionMode
屬性很容易地做這樣的事情,而不必讀取源像素顏色。
簡單的樣品QWidget
與自定義paintEvent
實施,你應該能夠適應你的項目的paint
方法:
#include <QtGui>
class W: public QWidget {
Q_OBJECT
public:
W(QWidget *parent = 0): QWidget(parent) {};
protected:
void paintEvent(QPaintEvent *) {
QPainter p(this);
// Draw boring background
p.setPen(Qt::NoPen);
p.setBrush(QColor(0,255,0));
p.drawRect(0, 0, 30, 90);
p.setBrush(QColor(255,0,0));
p.drawRect(30, 0, 30, 90);
p.setBrush(QColor(0,0,255));
p.drawRect(60, 0, 30, 90);
// This is the important part you'll want to play with
p.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination);
QPen inverter(Qt::white);
inverter.setWidth(10);
p.setPen(inverter);
p.drawLine(0, 0, 90, 90);
}
};
這將輸出類似下面的圖片:
試用其他composition modes以獲得更有趣的效果。
來源
2012-02-03 14:34:27
Mat
謝謝,工作完美。 – 2012-02-03 14:39:42