qml: ReferenceError: serial is not defined

qml编译警告:qrc:/Form02.qml:67: ReferenceError: serial is not defined

serial是我自定义的一个串口线程,在编译时报警告说serial未定义,但又能正常访问获取数据,使用代码示例如下:

示例代码:

cpp 复制代码
{
    //1
    ui->quickWidget_2->setSource(QUrl("qrc:/Form02.qml"));
    //2
    serial = new SerialPort;
    ui->quickWidget_2->rootContext()->setContextProperty("serial", serial);
    ui->quickWidget_2->setResizeMode(QQuickWidget::SizeRootObjectToView);
    //3 cpp 与 qml信号连接
    QObject* obj = ui->quickWidget_2->rootObject();
    if(QMetaObject::invokeMethod(obj, "slotOpenSerial", Qt::DirectConnection, Q_ARG(bool, true), Q_ARG(QString, "")))
        connect(serial, SIGNAL(openMsg(bool,QString)), obj, SIGNAL(slotOpenSerial(bool,QString)));
}

解决办法:先设置串口对象在根节点中的name,再将qml添加到quickWidget

cpp 复制代码
{
    //1
    serial = new SerialPort;
    ui->quickWidget_2->rootContext()->setContextProperty("serial", serial);
    ui->quickWidget_2->setResizeMode(QQuickWidget::SizeRootObjectToView);
    //2
    ui->quickWidget_2->setSource(QUrl("qrc:/Form02.qml"));
    //3 cpp 与 qml信号连接
    QObject* obj = ui->quickWidget_2->rootObject();
    if(QMetaObject::invokeMethod(obj, "slotOpenSerial", Qt::DirectConnection, Q_ARG(bool, true), Q_ARG(QString, "")))
        connect(serial, SIGNAL(openMsg(bool,QString)), obj, SIGNAL(slotOpenSerial(bool,QString)));
}
相关推荐
xcyxiner9 小时前
DicomViewer (后台线程处理文件)4
qt
xcyxiner16 小时前
DicomViewer (添加模型类)3
qt
xcyxiner1 天前
DicomViewer (目录调整) 2
qt
xcyxiner2 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能3 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G4 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
森G4 天前
77、线程池原理和实现------服务器源码解析----云视频服务项目
服务器·c++·qt
森G4 天前
71、打包发布---------打包发布
c++·qt
初圣魔门首席弟子4 天前
Node.js 详细介绍(知识库版)
windows·qt·node.js·知识库
C++ 老炮儿的技术栈4 天前
Qt工控实战:自研机器人TCP长连接客户端(粘包处理+心跳保活+自动重连完整源码解析)
qt·tcp/ip·机器人