What module should I use for streaming C++ QImage objects into QML?
-
Hi! I'm just start learning qt for developing desktop application. In particular, the front-end is implemented in QML and the back-end is implemented in C++.
My Qt6 app uses camera device. To control the camera device, I need to use the external C++ library provided by the camera vendor, so I can't use QCamera class of the QT Multimedia module.
So I made my own Camera class for handling streaming, which converts new raw frame to QImage and pushed it into a one-way queue. Whenever a new QImage is pushed in the queue, a signal emitted.
Now I want to create a slot that is connected to this signal and renders the QImage to QML.I've searched many forums, but I still can't figure it out. Some Qt5 examples use QAbstractVideoSurface, which seems to be no longer valid in Qt6. Some examples use QQuickImageProvider, but that doesn't seem like a good way to keep updating images.
Any advice will be appreciated
-
Hi and welcome to devnet,
Beside the controlling that you do with that external library, is the camera itself usable through QtMultimedia ?
-
@SGaist
I think no.
Just to add a little background, the camera communicates via GigE and the external library is ArenaSDK from Lucid Vision Labs, and the camera device can be controlled with the GenICam API. Also I need to handle multiple streaming cameras.I think when I import a physical camera device as an object from the external library, this camera device will not be looked up with
QMediaDevices::videoInputs()
.
If I implement a custom camera class that inherits fromQCameraDevice
and conforms to the GenICam API, I might be able to useQtMultimedia
. But it doesn't seem to provideQCameraDevice
as an abstract class.
By the way, I found a Qt5 example provided by the external library; Create a
QVideoFrame
usingQImage
and take it as an argument ofQAbstractVideoSurface::present()
.void pushImage(Arena::IImage* pImage) { if (m_isStreaming) { QVideoFrame videoFrame( QImage( (uchar*)(pImage->GetData()), pImage->GetWidth(), pImage->GetHeight(), QImage::Format::Format_RGB32)); m_pSurface->present(videoFrame); } }
Widgets are used instead of QML in this example, but I've seen some Qt5 examples in which
QAbstractVideoSurface
is binded into QML'sVideoOutput
.Only now did the question become a bit clearer.
- Constructing
QVideoFrame
withQImage
is deprecated in Qt6. Is there still a way to do it in Qt6? - If not, is there any other way to put
QImage
inVideoOutput
?
- Constructing
-
With Qt 5, you could have gone with a custom plugin or QMediaObject but I currently don't know for Qt 6.
Maybe something to bring to mailing list. Did you already check the bug tracker ?
-
This post is deleted!
-
@wooseokyourself If you have the QImage then copy the bits to QVideoFrame, something like:
QImage image(...); QVideoFrameFormat video_frame_format(image.size(), QVideoFrameFormat::pixelFormatFromImageFormat(image.format())); QVideoFrame video_frame(video_frame_format); if(video_frame.map(QVideoFrame::ReadWrite))){ std::memcpy(video_frame.bits(0), image.bits(), video_frame.mappedBytes(0)); video_frame.unmap(); }
-
@eyllanesc Thanks. Your solution works for converting QImage to QVideoFrame.
-
@SGaist Thanks for reply. But I don't understand what you meant "mailing list" and "bug tracker".
Thanks for @eyllanesc 's answer, I could convert QImage to QVideoFrame, but the next bigger problem is how to send C++'s QVideoFrame to QML's VideoOutput. So I didn't use QVideoFrame, but subclassing QQuickPaintedItem for QML's image object and bind it with my custom C++ image provider which update new QImage by using signal-slot.
I'm not sure it's best optimized solution, but quite fast as much as I expected. -
@wooseokyourself See this example: https://stackoverflow.com/questions/69432427/how-to-use-qvideosink-in-qml-in-qt6/69432938#69432938. In your case it is not necessary to use QImage, just use:
QSize size(pImage->GetWidth(), pImage->GetHeight()); QVideoFrameFormat video_frame_format(size, QVideoFrameFormat::pixelFormatFromImageFormat(QImage::Format_RGB32)); QVideoFrame video_frame(video_frame_format); if(video_frame.map(QVideoFrame::ReadWrite))){ std::memcpy(video_frame.bits(0), pImage->GetData(), video_frame.mappedBytes(0)); video_frame.unmap(); }
-
@wooseokyourself said in What module should I use for streaming C++ QImage objects into QML?:
@SGaist Thanks for reply. But I don't understand what you meant "mailing list" and "bug tracker".
The interest mailing list where you can find Qt's developers/maintainers.
The bug tracker is well "the place where the bugs are tracked".