4

I've been using QZXing to decode QR code, my QML looks like this

Image{ 
    width:300
    height:300
    id:imageToDecode
    source:"qr.jpg"
    cache: true
}

and when i pass it into C++ file, it has to be converted into a QImage type, in the code the original coder wrote like this

QGraphicsItem *item = qobject_cast<QGraphicsItem*>(imageObj);

and then use QGraphicsItem to construct a QImage,however this will always return 0 after conversion, since QGraphicsItem does not inherit QObject

straight conversion like

QImage*item = qobject_cast<QImage*>(imageToDecode);

will not work for same reason,ive been using

imageObj->inherits("objectName");

to test which one it can be converted to, but there is none i can find my point is to convert QML Image into QImage.

7
  • You can't cast QGraphicsItem to QImage, because it is very different data types. I suspect that you have QGraphicsPixmapItem (inheritor of QGraphicsItem ), so you can use QGraphicsPixmapItem::pixmap() to get your image (and after it convert it to QImage).
    – Ilya
    Commented Aug 16, 2014 at 4:27
  • Which version of Qt Quick, ie. what version you have in qml import QtQuick x.x? Which version of Qt? Just add relevant tags.
    – hyde
    Commented Aug 16, 2014 at 5:27
  • Also, your C++ snippets, are they real code? If yes, then show how these imageObj and imageToDecode are declared and initialized in C++, ie. what they are.
    – hyde
    Commented Aug 16, 2014 at 5:30
  • the way i wish to convert is like<code> Commented Aug 16, 2014 at 5:36
  • im using QtQuick 2.2, with Qt version 5.3, the imageObj is passsed from QML, which is a QML Image item, the way it is passed is exactly the way in the qzxing example, and i can actually get this item in C++, it looks fine, the only problem is after it is casted, its gone. (item=0) Commented Aug 16, 2014 at 5:40

2 Answers 2

2

If you are using Qt5 or greater and QtQuick 2.0 or greater you can't render the Image element to QGraphicsItem or anything related to QGraphics since from Qt5 the backend for QML is scenegraph and not QGraphics. So now going forward with Qt5.3 and you if access the Image element in C++, it will be casted to a QQuickImage internally, but still you can't extract image from Image element since QQuickImage has not been made public yet. Ref. http://qt-project.org/forums/viewthread/32767

The solution they have proposed for now is to use QQuickImageProvider.

4
  • Firstly, thanks for answering. Yea ive seen that thread, and ive realize this object is a QQuickImage and i can not extract anything from it. Maybe i didnt understood the way to use, but i think the QQuickImageProvider can only give image from C++ to QML, not the way back. Anyway, is there any solution that i can convert the QQuickImage to a QImage? Commented Aug 16, 2014 at 11:37
  • ` QGraphicsItem *item = qobject_cast<QGraphicsItem*>(imageObj); QImage img(item->boundingRect().size().toSize(), QImage::Format_RGB32); img.fill(QColor(255, 255, 255).rgb()); QPainter painter(&img); QStyleOptionGraphicsItem styleOption; item->paint(&painter, &styleOption);` apparently this is the way how it used to construct the QImage, i think i need to rewrite this part now, just having no idea on how to do it Commented Aug 16, 2014 at 11:39
  • As i said earlier there's no way, since QQuickImage API's are not public. If you stick with Qt Quick 2.0 or greater and Qt5 you cannot cast Image element to QGraphics*.
    – astre
    Commented Aug 16, 2014 at 11:44
  • Yes QQuickImageProvider gives image from C++ to QML, but anyhow you were going to comeback to C++ for processing ;)
    – astre
    Commented Aug 16, 2014 at 11:53
1

I found something that may (or may not) be a workaround to this issue.

Within the Image that is assigned the preview source, I call grabToImage() in order to create a QQuickItemGrabResult object and then in C++, I call the image() function on that object. It seems to work.

QML

Image {
  id:imageToDecode
  MouseArea {
    anchors.fill:parent
    onClicked: parent.grabToImage(function(result)
      decoder.decodeImageQML(result); })
    }
 }

C++

QImage ImageHandler::extractQImage(QObject *imageObj,
                     const double offsetX, const double offsetY,
                     const double width, const double height)
{
    QQuickItemGrabResult *item = 0;
    item = qobject_cast<QQuickItemGrabResult*>(imageObj);
    QImage item_image(item->image());
    ....
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.