You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在QML窗口中嵌入已实现的RealTimeMultiChart QWidget?

问题描述

我在主窗口中调用独立QML组件,想把指定图表原封不动嵌入QML里,请问可行吗?提前致谢。

当前启动QML主窗口的代码:

QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QUrl("qrc:/QmlSources/main.qml")));
return app.exec();

目前我能通过RealTimeMultiChart realTimeMultiChart; realTimeMultiChart.exec();在独立窗口打开该图表,RealTimeMultiChart的构造函数代码如下:

//
// Constructor
//
RealTimeMultiChart::RealTimeMultiChart(QWidget *parent) :
    QDialog(parent)
{
    //
    // Set up the GUI
    //

    setFixedSize(772, 60 + 3 * chartHeight + xAxisHeight);
    setWindowTitle("Real-Time MultiChart with Zoom/Scroll and Track Line");

    QFrame *frame = new QFrame(this);
    frame->setGeometry(4, 4, 120, height() - 8);
    frame->setFrameShape(QFrame::StyledPanel);

    // Chart Viewer
    m_ChartViewer = new QChartViewer(this);
    m_ChartViewer->setGeometry(128, 4, 640, 350);
    connect(m_ChartViewer, SIGNAL(viewPortChanged()), SLOT(onViewPortChanged()));
    connect(m_ChartViewer, SIGNAL(mouseMovePlotArea(QMouseEvent*)),
        SLOT(onMouseMovePlotArea(QMouseEvent*)));

    // Horizontal scroll bar
    m_HScrollBar = new QScrollBar(Qt::Horizontal, this);
    m_HScrollBar->setGeometry(128, height() - 18, 640, 18);
    connect(m_HScrollBar, SIGNAL(valueChanged(int)), SLOT(onHScrollBarChanged(int)));

    // Clear data arrays to Chart::NoValue
    for (int i = 0; i < sampleSize; ++i)
        m_timeStamps[i] = m_dataSeriesA[i] = m_dataSeriesB[i] = m_dataSeriesC[i] = Chart::NoValue;
    m_currentIndex = 0;

    // Set m_nextDataTime to the current time. It is used by the real time random number
    // generator so it knows what timestamp should be used for the next data point.
    m_nextDataTime = QDateTime::currentDateTime();

    // Initially set the mouse to drag to scroll mode.
    pointerPB->click();

    // Enable mouse wheel zooming by setting the zoom ratio to 1.1 per wheel event
    m_ChartViewer->setMouseWheelZoomRatio(1.1);

    // Set up the data acquisition mechanism. In this demo, we just use a timer to get a
    // sample every 250ms.
    QTimer *dataRateTimer = new QTimer(this);
    dataRateTimer->start(DataInterval);
    connect(dataRateTimer, SIGNAL(timeout()), SLOT(onDataTimer()));

    // Set up the chart update timer
    m_ChartUpdateTimer = new QTimer(this);
    connect(m_ChartUpdateTimer, SIGNAL(timeout()), SLOT(onChartUpdateTimer()));

    // Can start now
    m_ChartUpdateTimer->start();
}

期望嵌入的图表效果:
实时多图表(带缩放/滚动和跟踪线)

可行,具体实现步骤

1. 修改C++启动代码,传递图表实例给QML

不要调用exec()启动独立对话框,直接创建图表实例并注入QML上下文:

QApplication app(argc, argv);
QQmlApplicationEngine engine;

// 创建图表实例,无需调用exec()
RealTimeMultiChart* chartWidget = new RealTimeMultiChart();
// 将实例注入QML上下文,QML中可通过"realTimeChart"访问
engine.rootContext()->setContextProperty("realTimeChart", chartWidget);

engine.load(QUrl("qrc:/QmlSources/main.qml"));
return app.exec();

2. 在QML中嵌入Qt Widget

在QML文件导入QtQuick.widgets模块,用Widget组件承载图表:

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.widgets 1.15

Rectangle {
    width: 800
    height: 600

    // 嵌入图表Widget
    Widget {
        anchors.fill: parent
        widget: realTimeChart // 对应C++传递的实例
    }
}

3. 检查项目配置

确保.pro文件包含必要的Qt模块:

QT += core gui widgets qml quick

注意事项

  • 禁止调用realTimeMultiChart.exec(),否则会弹出独立对话框,直接注入QML即可正常运行。
  • 图表内的信号槽只要符合Qt元对象规则,无需额外修改就能正常工作。

内容的提问来源于stack exchange,提问作者Hma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 01:20:19