4

我想要实现的是以下内容:我有QGraphicsScene一个QGraphicsPixmapItem显示在其中的。像素图有多种颜色,我需要在像素图上画一条线,该线必须在每个点上都可见且可识别。

我的想法是画一条线,其中每个像素都具有像素图相对像素的负(互补)颜色。所以我想到了子类QGraphicsItem化并重新实现paint()绘制多色线的方法。

但是我被卡住了,因为我不知道如何从paint函数中检索像素图的像素信息,即使我发现了,我也想不出用这种方式画线的方法。

你能给我一些关于如何进行的建议吗?

4

1 回答 1

12

您可以使用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);
        }
};

这将输出类似于下图的内容:

时髦颜色上的脂肪倒线

尝试其他合成模式以获得更有趣的效果。

于 2012-02-03T14:34:27.860 回答