我想要实现的是:我有一个显示了QGraphicsPixmapItem
的QGraphicsScene
。像素图有多种颜色,我需要在像素图上画一条线,这条线必须在每个单独的点上都是可见和可识别的。
我的想法是绘制一条线,其中每个像素都具有像素图相对像素的负(互补)颜色。因此,我考虑对QGraphicsItem
进行子类化,并重新实现paint()
方法来绘制一条多色线。
然而,我被卡住了,因为我不知道如何从paint
函数中检索像素图的像素信息,即使我发现了,我也想不出一种方法来以这种方式绘制直线。
你能给我一些关于如何进行的建议吗?
发布于 2012-02-03 14:34:28
您可以使用QPainter
的compositionMode
属性非常容易地完成类似的操作,而不必读取源像素颜色。
带有自定义paintEvent
实现的简单示例QWidget
,您应该能够适应项目的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进行实验,以获得更有趣的效果。
https://stackoverflow.com/questions/9129955
复制相似问题