这可以通过使用QQuickPaintedItem来完成。
在C++的Qt部分
你需要有它继承QQuickPaintedItem类:
//imagewriter.h
class imagewriter : public QQuickPaintedItem
{
Q_OBJECT
public:
imagewriter(QQuickItem *parent = 0);
void paint(QPainter *painter);
//the paint method is already implemented in QQuickPaintedItem
//you just override it
QImage image; //your image
};
// imagewriter.cpp
imagewriter::imagewriter(QQuickItem *parent): QQuickPaintedItem(parent)
{
}
void imagewriter::paint(QPainter *painter)
{
painter->drawImage(QPoint(0,0),this->image);
//this will simply draw the image in your qml UI !
}
现在您只需注册该对象以便能够在您的qml文件中使用它:
//主的.cpp:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<imagewriter>("mr.jb",1,0,"my_video_image");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
// main.qml:
import mr.jb 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
my_video_image {
id : my_img_strm1
anchors.centerin : parent
}
}
的my_video_image对象应该能够显示您的QImage,所有你需要的要做的就是不断更新该图像到你从ROS流获得的最后一幅图像,并且每次你调用预定义的update();方法,以便自动重新触发paint方法,该方法可响应在qml世界中绘制图像。 我想你没有T1部分的问题。
在T2(C++类)中接收和构建图像可以通过多种方式完成(例如QUdp)。
最后一部分(T3)在上面的代码中描述。
请告诉我,如果你仍然无法做到这一点 – Kornava