本文主要是介绍在Qml中使用QCustomPlot,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
QmlQCustomPlot 基于 QCustomPlot,一个支持在 Qml 中使用的简单包
🔔工程下载
GitHub 源码下载:https://github.com/MrHulu/QmlQCustomPlot
CSDN 资源下载 (免费) :https://download.csdn.net/download/qq_41898196/89450306
🔔快速开始
使用cmake构建
在你的工程目录内创建一个目录, 假设你为改目录命名为QmlQCustomPlot, 本仓库的src
目录是一个独立的模块, 可直接拷贝src
里面的所有文件到你的QmlQCustomPlot
目录内, 然后在你的项目适当的CMakeList.txt里链接这个模块即可
使用qmake构建
在你的工程目录内创建一个目录, 假设你为改目录命名为QmlQCustomPlot, 拷贝src
里面的所有文件(除了CMakeLists.txt
)到你的QmlQCustomPlot
目录内, 创建一个名为QmlQCusomPlot.pri
的文件, 内容如下:
# QmlQCusomPlot.priHEADERS += $$files(*.h)
SOURCES += $$files(*.cpp *.cc)QT += core gui quick widgets printsupportQTPLUGIN += qmlplugin
QML_IMPORT_PATH = $$PWD
QML_FILE_EXTENSIONS += .qml# Setting the C++ standard (C++11 is assumed to be used here, adjust as appropriate)
CONFIG += c++11# Linking Qt libraries
LIBS += -lQt5Core \-lQt5Gui \-lQt5Quick \-lQt5Widgets \-lQt5PrintSupport
接下来,在你的.pro文件中包含这个.pri文件,例如,如果你的项目名为MyProject.pro
,则在该文件中添加以下行:
# MyProject.proTEMPLATE = app
TARGET = MyProject# Include QmlQCustomPlot modules
include(QmlQCusomPlot.pri)# Other...
🔔讨论
截止到2024年05月27日,目前作者还没有实现直接支持qml的QCustomPlot,但是我联系了作者,作者有说在实施但是具体上线时间没有一个实际的计划。 QCustomPlot源码中虽然很多属性使用了Q_PROPERTY
,但是没有信号导致无法对QML的属性绑定提供支持。所以封装为可以提供Qml使用的类需要的工作量巨大😭,我只封装了一部分,如果无法满足你的需求,可以直接修改源码,应该很简单。 一些其他的参考:https://www.qcustomplot.com/index.php/support/forum/172
🔔UML
1
*
1
4
1
1
1
1
Graph
- QCustomPlot* m_parentPlot
- QCPGraph* m_graph
Q_PROPERTY(bool visible READ visible WRITE setVisible NOTIFY visibleChanged)
Q_PROPERTY(bool antialiased READ antialiased WRITE setAntialiased NOTIFY antialiasedChanged)
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(LineStyle lineStyle READ lineStyle WRITE setLineStyle NOTIFY lineStyleChanged)
Q_PROPERTY(QColor graphColor READ graphColor WRITE setLineColor NOTIFY graphColorChanged)
Q_PROPERTY(int graphWidth READ graphWidth WRITE setLineWidth NOTIFY graphWidthChanged)
Q_ENUM(LineStyle)
enum LineStyle(lsNone, lsLine, lsStepLeft, lsStepRight, lsStepCenter, lsImpulse)
+Graph(QCustomPlot* parentPlot)
+~Graph()
+void setData(const QVector& x, const QVector& y)
+void addData(double x, double y)
+void removeDataBefore(double x)
+void clearData()
-updateProperty()
QObject
Axis
- QCustomPlot* m_parentPlot
- QCPAxis* m_axis
- Grid* m_grid
- Ticker* m_ticker
Q_PROPERTY(bool visible READ visible WRITE setVisible NOTIFY visibleChanged)
Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
Q_PROPERTY(float upper READ upper WRITE setUpper NOTIFY upperChanged)
Q_PROPERTY(float lower READ lower WRITE setLower NOTIFY lowerChanged)
Q_PROPERTY(Grid* grid READ grid CONSTANT)
Q_PROPERTY(Ticker* ticker READ Ticker NOTIFY TickerChanged)
Q_ENUM(TickerType)
enum TickerType(Fixed, Log, Pi, Text, DateTime, Time)
+Axis(QCPAxis* axis, QObject* parent)
+~Axis()
+setTickerType(TickerType type)
+setRange(float position, float size, Qt::AlignmentFlag align)
+setTicker(QSharedPointer ticker)
-updateProperty()
Grid
- QCustomPlot* m_parentPlot
- QCPGrid* m_qcpgrid
Q_PROPERTY(bool visible READ visible WRITE setVisible NOTIFY visibleChanged)
Q_PROPERTY(bool subVisible READ subVisible WRITE setSubVisible NOTIFY subVisibleChanged)
Q_PROPERTY(int lineWidth READ lineWidth WRITE setLineWidth NOTIFY lineWidthChanged)
Q_PROPERTY(QColor lineColor READ lineColor WRITE setLineColor NOTIFY lineColorChanged)
Q_PROPERTY(LineType lineType READ lineType WRITE setLineType NOTIFY lineTypeChanged)
Q_PROPERTY(int subLineWidth READ subLineWidth WRITE setSubLineWidth NOTIFY subLineWidthChanged)
Q_PROPERTY(QColor subLineColor READ subLineColor WRITE setSubLineColor NOTIFY subLineColorChanged)
Q_PROPERTY(LineType subLineType READ subLineType WRITE setSubLineType NOTIFY subLineTypeChanged)
Q_ENUM(LineType)
enum LineType(NoPen, SolidLine, DashLine, DotLine, DashDotLine, DashDotDotLine)
+Grid(grid, QCustomPlot* parentPlot)
~Grid()
-updateProperty()
Ticker
- QCustomPlot* m_parentPlot
- QSharedPointer m_ticker
Q_PROPERTY(bool ticks READ ticks WRITE setTicks NOTIFY ticksChanged)
Q_PROPERTY(bool subTicks READ subTicks WRITE setSubTicks NOTIFY subTicksChanged)
Q_PROPERTY(int tickCount READ tickCount WRITE setTickCount NOTIFY tickCountChanged)
Q_PROPERTY(QColor tickColor READ tickColor WRITE setTickColor NOTIFY tickColorChanged)
Q_PROPERTY(QColor subTickColor READ subTickColor WRITE setSubTickColor NOTIFY subTickColorChanged)
Q_PROPERTY(QColor baseColor READ baseColor WRITE setBaseColor NOTIFY baseColorChanged)
Q_PROPERTY(int baseWidth READ baseWidth WRITE setBaseWidth NOTIFY baseWidthChanged)
+Ticker(QSharedPointer ticker, QCustomPlot* parentPlot)
+~Ticker()
-updateProperty()
BasePlot
- QCustomPlot* m_customPlot
- QMap m_graphs
Q_PROPERTY(Axis* xAxis READ xAxis CONSTANT)
Q_PROPERTY(Axis* x1Axis READ x1Axis CONSTANT)
Q_PROPERTY(Axis* yAxis READ yAxis CONSTANT)
Q_PROPERTY(Axis* y1Axis READ y1Axis CONSTANT)
Q_PROPERTY(QVariantMap graphs READ graphs NOTIFY graphsChanged)
Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
+BasePlot(QQuickItem* parent)
+~BasePlot()
+void addGraph(const QString& key)
+void removeGraph(const QString& key)
+void rescaleAxes(bool onlyVisiblePlottables=false)
+void paint(QPainter *painter)
+QCustomPlot *customPlot()
+const QMap &graphsMap()
+Graph* getGraph(const QString& key)
#void onChartViewReplot()
#void onChartViewSizeChanged()
#virtual void hoverMoveEvent(QHoverEvent *event)
#virtual void mousePressEvent(QMouseEvent* event)
#virtual void mouseReleaseEvent(QMouseEvent* event)
#virtual void mouseMoveEvent(QMouseEvent* event)
#virtual void mouseDoubleClickEvent(QMouseEvent* event)
#virtual void wheelEvent(QWheelEvent *event)
#void routeMouseEvents(QMouseEvent* event)
#void routeWheelEvents(QWheelEvent* event)
QQuickPaintedItem
TimePlot
- QTimer* m_timer
- qint64 m_lastAddedTime
- qint64 m_lastClearTime
Q_PROPERTY(int plotTimeRangeInMilliseconds READ plotTimeRangeInMilliseconds WRITE set_plotTimeRangeInMilliseconds NOTIFY plotTimeRangeInMillisecondsChanged)
+TimePlot(QQuickItem* parent)
+~TimePlot()
+setTimeFormat(const QString &format)
+addCurrentTimeValue(const QString& name, double value)
+addCurrentTimeValues(QVariantMap values)
#virtual void timerEvent(QTimerEvent *event)
#virtual void onTimeOut()
这篇关于在Qml中使用QCustomPlot的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!