目前,我正在使用main.cpp
文件中的socketcan API实时接收CAN数据。
我正在main.cpp
中的一个变量中不断更新CAN的数据帧。
我想通过将main.cpp
中包含CAN数据帧的变量传递到QML动画量规来实时表示量规。
我需要实时检测QML中包含CAN数据的变量的变化。我想知道是否有有效的方法。
我尝试使用emit
与QML共享数据。
但是,emit
内部的device-> connect (device, & QCanBusDevice :: framesReceived, [device] () {...}
编写的功能不起作用。
使用它时出现错误
在这种情况下,不能隐式捕获'
this
'。
我查找了错误,但没有找到答案。
if (QCanBus::instance()->plugins().contains(QStringLiteral("socketcan"))) {
qWarning() << "plugin available";
}
QString errorString;
QCanBusDevice *device = QCanBus::instance()->createDevice(
QStringLiteral("socketcan"), QStringLiteral("vcan0"), &errorString);
if (!device) {
qWarning() << errorString;
} else {
device->connectDevice();
std::cout << "connected vcan0" << std::endl;
device->connect(device, &QCanBusDevice::framesReceived, [device]() {
QCanBusFrame frame = device->readFrame();
QString testV = frame.toString();
QString qvSpeed = frame.payload();
std::string text = testV.toUtf8().constData();
std::string vSpeed = qvSpeed.toUtf8().constData();
//At that point the vVal values are being updated in real time.
//I want to pass the updated vVal to qml gui in real time.
int vVal = static_cast<int>(frame.payload()[0]);
//emit sendMessage(vVal); // 'this' cannot be implicitly captured in this context error.
std::cout << text << std::endl;
});
}
到目前为止,main.cpp
无法发送数据,QML无法解决错误。
在device-> connect
内,emit sendMessage (vVal);
将导致“无法在此上下文中隐式捕获'this'”错误。
我想知道是否存在通过实时表达QML GUI数据来实现动画的好方法。
答案 0 :(得分:1)
您的捕获子句仅捕获device
。您还需要明确捕获this
:
device->connect(device, &QCanBusDevice::framesReceived, [this,device]{ /*...*/ });
顺便说一句,请注意,无需为无参数的lambda表达式指定()
。