I have a QQuickControls 2 application that displays an image using a QQuickImageProvider
. Sometimes I will simply want to show an unedited image so I just implement my QML like so:
Image {
id: image
fillMode: Image.PreserveAspectFit
source: "image://provider/foo/bar/placeholder.jpg"
}
Other times I will want to show an edited version of the image. How can I tell QQuickImageProvider::requestImage()
I want to show an edited version?
QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
// Somehow determine we need to show an editted version of the image
if (showEdited) {
// maybe pass query parameters to the id?
// for eg 'image://provider/foo/bar/i.jpg?edit=true'
// Then I parse the id string for this query parameter?
cv::Mat src = cv::imread(id.toStdString());
// ... perform some image processing to the image
QImage img = convertMatToQImage(src);
if (size)
*size = QSize(img.width(), img.height());
return img;
}
else {
QImage img(id);
if (size)
*size = QSize(img.width(), img.height());
return img;
}
}
The trigger to show the edited image is by clicking a button:
Button {
id: processBtn
text: qsTr("Process")
onClicked: {
// Somehow call QQuickImageProvider::requestImage() and specify we are editting it?
// Maybe...
// image.source = image.source + "?edit=true"
}
}
id
, for example asimage
orimage-edited
and parse it insiderequestImage()
or, that would be right, create 2 image providers and use that you need. – folibis