在Qt中绘制一条多彩的线

4
我想要实现的是:我有一个显示了QGraphicsPixmapItemQGraphicsScene。该像素图具有多种颜色,我需要在像素图上绘制一条线,使每个点都可见且易于识别。
我的想法是绘制一条线,其中每个像素都具有像素图相对像素的负(补色)颜色。因此,我考虑子类化QGraphicsItem并重新实现paint()方法以绘制多彩的线条。
但是,我卡住了,因为我不知道如何从paint函数中检索像素图的像素信息,即使我找到了,我也想不出以这种方式绘制线条的方法。
你能给我一些关于如何继续的建议吗?
1个回答

12
你可以使用 QPaintercompositionMode 属性轻松实现此类操作,而无需读取源像素颜色。
以下是一个简单的 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);
        }
};

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

Fat inverted line over funky colors

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接