首页 > 解决方案 > 当 C++ SLOT 函数尝试与从 QML 获得的输入进行交互时崩溃

问题描述

我有这样的事情导致我的程序崩溃

void BackEnd::updateChart(QtCharts::QChartView *view)
{
    QList<QObject*> children = view->children();
}

在 QML 方面:

backend.updateChart(chartView);

其中“backend”是 QT 对象“BackEnd”的一个实例,“chartView”是 QML 类型“ChartView”的一个实例

我尝试在我的 BackEnd 构造函数上运行以下命令,但这并没有帮助

qRegisterMetaType<QtCharts::QChartView*>();

标签: c++qtqmlqt5

解决方案


ChartView is not a QChartView, ChartView is a DeclarativeChart that is a QQuickItem of the private api that handles the same data of QChartView but it is not a QChartView. So the solution is to change QtCharts::QChartView to QQuickItem or QObject.

void BackEnd::updateChart(QQuickItem /*or QObject*/ *view)
{
    QList<QObject*> children = view->children();
}

if you print view with qDebug() you get:

QtCharts::DeclarativeChart(0xfoo_address, parent=0x0, geometry=0,0 400x300)

推荐阅读