2012-02-03 189 views
3

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

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

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

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

回答

11

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

试用其他composition modes以获得更有趣的效果。

+0

谢谢,工作完美。 – 2012-02-03 14:39:42