QGraphicsPathItem:以不同方式绘制元素

时间:2016-06-15 15:12:23

标签: qt qt5 qgraphicsitem

如何绘制export enum TextAlign { Left = <any>"start", Right = <any>"end", Center = <any>"middle" } 以使其在最后的n元素之后淡出的方式?

每一秒我都会在路径的末尾添加QGraphicsPathItem并希望显示具有100%不透明度的第n个元素。 n-1为90%,n-2为80%等。因此路径仅显示最后10个(例如)并平滑淡出。但是我怎么能用qt5呢?

我想到了如何改变整个路径的颜色,但是如何在子项级别上更改它...这里的路径元素是什么意思?

2 个答案:

答案 0 :(得分:2)

我会为每个细分使用单独的QGraphicsLineItem并单独调整它们的不透明度:

class Object : public QObject {
  Q_OBJECT
public:
  Object();
  void add_point(QPointF point);
private:
  QGraphicsScene m_scene;
  QList<QGraphicsLineItem*> m_items;
  QPointF m_previous_point;
private slots:
  void timeout();
};

Object::Object() {
  QGraphicsView* view = new QGraphicsView();
  view->setRenderHint(QPainter::Antialiasing);
  view->setScene(&m_scene);
  view->scale(10, 10);
  view->resize(400, 200);
  view->show();
  QTimer* timer = new QTimer(this);
  timer->start(30);
  connect(timer, &QTimer::timeout, this, &Object::timeout);
}

void Object::add_point(QPointF point) {
  const int MAX_SEGMENTS = 100;
  QGraphicsLineItem* new_line = 
      m_scene.addLine(QLineF(m_previous_point, point));
  QPen pen;
  pen.setCosmetic(true);
  pen.setWidth(3);
  new_line->setPen(pen);
  m_items.append(new_line);
  while(m_items.count() > MAX_SEGMENTS) {
    delete m_items[0];
    m_items.removeFirst();
  }
  for(int i = 0; i < m_items.count(); i++) {
    m_items[i]->setOpacity(
        1.0 - 1.0 / MAX_SEGMENTS * (m_items.count() - 1 - i));
  }
  m_previous_point = point;
}

void Object::timeout() {
  double x = m_previous_point.x() + 0.1;
  add_point(QPointF(x, qSin(x)));
}

答案 1 :(得分:1)

重新实现QGraphicsPathItem::paint()方法(如Kuba所述)并使用QBrushQLinearGradient逐段绘制路径。

如果需要,我可以更具体。