基于QtQuick的QCustomPlot实现

简介: 本文介绍了如何在QtQuick中实现基于QCustomPlot的图表绘制,包括效果图展示、C++和QML方面的实现代码、注意事项以及应用场景。作者提供了源码下载链接,方便读者学习和使用QCustomPlot进行QtQuick应用程序中的图表绘制。

前言

QtQuick开发需要C++基础和JS基础
掌握必要的C++基础是学习和使用QtQuick框架的重要基石

QCustomPlot能做什么
可以在QWidget大放异彩,绘制各种图表和曲线

也可以在QtQuick大展拳脚

  • 基于QtQuick的QCustomPlot实现
  • 继承QQuickPaintedItem类 实现绘制

效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

class DDuiBasePlotItem : public QQuickPaintedItem
{
    Q_OBJECT
public:
    DDuiBasePlotItem( QQuickItem* parent = 0 );
    virtual ~DDuiBasePlotItem();
    void paint( QPainter* painter );
    virtual void initCustomPlotUI(){}

    QCustomPlot *getPlot();
protected:
    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 );
public slots:
    void graphClicked( QCPAbstractPlottable* plottable );
    void onCustomReplot();
    void updateCustomPlotSize();
private:
    QCustomPlot*         m_CustomPlot;
};
///
/// \brief DDuiCTPWidget::connectQuoteServer
/// 初始化
/// 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    qmlRegisterType<CustomPlotItem>("CustomPlot", 1, 0, "CustomPlotItem");
    qmlRegisterType<CustomPlotBarItem>("CustomPlot", 1, 0, "CustomPlotBarItem");
    qmlRegisterType<CustomKLine>("CustomPlot", 1, 0, "CustomKLine");//CustomColorMap
    qmlRegisterType<CustomColorMap>("CustomPlot", 1, 0, "CustomColorMap");
    qmlRegisterType<CustomBarChart>("CustomPlot", 1, 0, "CustomBarChart");

    QQmlApplicationEngine engine;
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));

    return a.exec();
}

QML方面实现

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtQuick.XmlListModel 2.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("QCustomPlot在QML中使用")
    SwipeView {
        id: swipeView
        anchors.fill: parent
        currentIndex: tabBar.currentIndex
        interactive: false

        DDRubberband{
        }
        PlotView {
        }

        CustomPlotBar{

        }
        CustomKLine{

        }
        CustomColorMap{

        }
        CustomBarCharts{}
        Page {
            Label {
                text: qsTr("This is implementation of http://www.qcustomplot.com/index.php/support/forum/172\n" +
                           "Adding random data on 500 ms tick to plot")
                anchors.centerIn: parent
            }
            XmlListModel {
                id: xmlModel
                source: "qrc:/combox.xml"
                query: "/rss/item"
                XmlRole { name: "title"; query: "title/string()" }
                XmlRole { name: "pubDate"; query: "pubDate/string()" }
            }
            ComboBox {
                textRole: "title"
                model: xmlModel
            }
        }
    }

    footer: TabBar {
        id: tabBar
        currentIndex: swipeView.currentIndex
        TabButton {
            text: qsTr("RubberBand")
        }
        TabButton {
            text: qsTr("Plot")
        }
        TabButton {
            text: qsTr("Bar")
        }
        TabButton {
            text: qsTr("KLine")
        }
        TabButton {
            text: qsTr("ColorMap")
        }
        TabButton {
            text: qsTr("BarChart")
        }
        TabButton {
            text: qsTr("Info")
        }
    }
}

注意事项

* 需要QCustomPlot源码
* 需要自己继续基于例子进行开发自己需要的图表

应用场景

  • 方便QCustomPlot业务移植到QtQuick应用程序上面

源文件下载

源码下载链接源码

相关文章
|
算法 数据可视化 前端开发
第三代软件开发-QCustomPlot核心迁移
欢迎来到我们的 QML & C++ 项目!这个项目结合了 QML(Qt Meta-Object Language)和 C++ 的强大功能,旨在开发出色的用户界面和高性能的后端逻辑。 在项目中,我们利用 QML 的声明式语法和可视化设计能力创建出现代化的用户界面。通过直观的编码和可重用的组件,我们能够迅速开发出丰富多样的界面效果和动画效果。同时,我们利用 QML 强大的集成能力,轻松将 C++ 的底层逻辑和数据模型集成到前端界面中。 在后端方面,我们使用 C++ 编写高性能的算法、数据处理和计算逻辑。C++ 是一种强大的编程语言,能够提供卓越的性能和可扩展性。我们的团队致力于优化代码,减少资
|
前端开发 JavaScript C++
打造卓越 QML 层级设计:从入门到精通(一)
打造卓越 QML 层级设计:从入门到精通
3524 1
|
数据可视化 API vr&ar
探索Qt 3D之旅:从基础到实战,打造引人入胜的三维界面与应用
探索Qt 3D之旅:从基础到实战,打造引人入胜的三维界面与应用
2467 3
|
设计模式 编解码 前端开发
打造卓越 QML 层级设计:从入门到精通(三)
打造卓越 QML 层级设计:从入门到精通(三)
1745 0
|
前端开发 JavaScript C++
QML信号与信号槽实践指南:轻松掌握现代软件开发的关键技术(一)
QML信号与信号槽实践指南:轻松掌握现代软件开发的关键技术
666 0
qml import 自定义模块 cmake
qml import 自定义模块 cmake
745 1
QML 界面切换的方法
QML 界面切换的方法
796 1
|
编译器 开发工具 C语言
解锁QtCreator跨界神技!Windows下轻松驾驭OpenCV动态库,让你的跨平台开发如虎添翼,秒变视觉编程大师!
【8月更文挑战第4天】QtCreator是一款强大的跨平台IDE,便于创建多平台应用。本教程教你如何在Windows环境下集成OpenCV库至Qt项目。首先,下载匹配MinGW的OpenCV预编译版并解压。接着,在QtCreator中新建或打开项目,并在.pro文件中添加OpenCV的头文件和库文件路径。确保编译器设置正确。随后编写测试代码,例如加载和显示图片,并进行编译运行。完成这些步骤后,你就能在QtCreator中利用OpenCV进行图像处理开发了。
692 6
|
算法 C++ 开发者
【QML文件结构】理解QML中 多层嵌套控件之间的关系
【QML文件结构】理解QML中 多层嵌套控件之间的关系
1090 2