QCPLayer 一个可能包含对象的层,用于控制渲染顺序。

QCustomPlot的分层系统用来控制绘图中元素渲染顺序。它基于QCPLayer和QCPLayerable两个类。QCustomPlot保存一个或多个QCPLayer实例的有序列表(请参见QCustomPlot::addLayer、QCustomPlot::layer、QCustomPlot::moveLayer等)。重新绘图时,QCustomPlot会从下到上遍历图层列表,并将图层的图层依次绘制到绘图缓冲区中。
QCPLayer包含QCPLayers实例的有序列表。QCPLayerable是一个抽象基类,几乎所有可见对象都从中派生出来,如轴(axies、网格(grid)、图形(graphs)、项目等。

  • qcplayer默认层默认层

最初,QCustomPlot有六层:“background”、“grid”、“main”、“axis”、“legend”和“overlay”(按此顺序)。顶部是“覆盖”层,它只包含QCustomPlot的选择区域(QCustomPlot::selectRect)。接下来的两个图层“axes”和“legend”包含默认axes 和 legend,因此它们将绘制在可绘图对象上方。在中间,有一个“main”层。它最初为空,并设置为当前图层(请参见QCustomPlot::setCurrentLayer)。这意味着,默认情况下,所有新的(plottables)绘图对象、items等都是在该图层上创建的。然后是“网格”层,其中包含QCPGrid实例(它们紧密地属于QCPAxis,请参阅QCPAxis::grid)。Axis rect背景应绘制在其他所有内容后面,因此默认的QCPAxisLect实例放置在“背景”层上。当然,可以根据需要更改单个对象的层从属关系(QCPLayerable::setLayer)。

  • qcplayer排序部分通过图层控制渲染顺序

控制绘图中图层的顺序很容易:使用QCustomPlot::addLayer在您希望图层所在的位置(例如“main”上方)创建一个新图层。然后使用QCustomPlot::setCurrentLayer将当前图层设置为新图层,最后正常创建对象。由于当前的图层设置,它们将自动放置在新图层上。或者,您也可以忽略当前的图层设置,在创建对象后将具有QCPLayerable::setLayer的对象移动到所需的图层。也可以移动整个层。例如,如果您希望网格显示在“main”层上的所有可绘图项/项目之前,只需使用QCustomPlot::moveLayer将其移动到“main”上方即可。一个层内的渲染顺序只是按创建或插入的顺序。最后创建的项目(或最后添加到图层的项目)绘制在该图层上的所有其他对象之上。
删除图层时,其上的对象不会随之删除,而是落在已删除图层下方的图层上,请参见QCustomPlot::removeLayer。

  • qcplayer 仅重新绘制特定图层

如果层模式(setMode)设置为lmBuffered,则可以通过调用replot仅重新绘制此特定层。在某些情况下,与所有层的完整重绘相比,这可以提供更好的重绘性能。创建新层后,层模式初始化为lmLogical。在新的QCustomPlot实例中,唯一设置为lmBuffered的层是“覆盖”层,其中包含选择区域。

qcustomplot.cpp资源-CSDN下载

class QCP_LIB_DECL QCPLayer : public QObject
{
  Q_OBJECT
   
  Q_PROPERTY(QCustomPlot* parentPlot READ parentPlot)
  Q_PROPERTY(QString name READ name)
  Q_PROPERTY(int index READ index)
  Q_PROPERTY(QList<QCPLayerable*> children READ children)
  Q_PROPERTY(bool visible READ visible WRITE setVisible)
  Q_PROPERTY(LayerMode mode READ mode WRITE setMode)
 
public:
  enum LayerMode { lmLogical   ///< Layer is used only for rendering order, and shares paint buffer with all other adjacent logical layers.
                   ,lmBuffered ///< Layer has its own paint buffer and may be replotted individually (see \ref replot).
                 };
  Q_ENUMS(LayerMode)
  
  QCPLayer(QCustomPlot* parentPlot, const QString &layerName);
  virtual ~QCPLayer();
  
  // getters:
  QCustomPlot *parentPlot() const { return mParentPlot; }
  QString name() const { return mName; }
  int index() const { return mIndex; }
  QList<QCPLayerable*> children() const { return mChildren; }
  bool visible() const { return mVisible; }
  LayerMode mode() const { return mMode; }
  
  // setters:
  void setVisible(bool visible);
  void setMode(LayerMode mode);
  
  
  void replot();
  
protected:
  // property members:
  QCustomPlot *mParentPlot;
  QString mName;
  int mIndex;
  QList<QCPLayerable*> mChildren;
  bool mVisible;
  LayerMode mMode;
  
  // non-property members:
  QWeakPointer<QCPAbstractPaintBuffer> mPaintBuffer;
  
  // non-virtual methods:
  void draw(QCPPainter *painter);
  void drawToPaintBuffer();
  void addChild(QCPLayerable *layerable, bool prepend);
  void removeChild(QCPLayerable *layerable);
  
private:
  Q_DISABLE_COPY(QCPLayer)
  
  friend class QCustomPlot;
  friend class QCPLayerable;
};
Q_DECLARE_METATYPE(QCPLayer::LayerMode)

Logo

openvela 操作系统专为 AIoT 领域量身定制,以轻量化、标准兼容、安全性和高度可扩展性为核心特点。openvela 以其卓越的技术优势,已成为众多物联网设备和 AI 硬件的技术首选,涵盖了智能手表、运动手环、智能音箱、耳机、智能家居设备以及机器人等多个领域。

更多推荐