How to Send QML image to C++ side
Solved
QML and Qt Quick
-
I want to send the image from qml to c++ to be stored in the SQL database
This is my code, but when I click the save button, my program crashes
How can I fix it?c++ side:
void ImageProvider::saveToDatabase(QObject *imageObj) { QQuickItemGrabResult *item = nullptr; item = qobject_cast<QQuickItemGrabResult *>(imageObj); QImage img(item->image()); QByteArray imageArray; QBuffer buffer(&imageArray); buffer.open(QIODevice::WriteOnly); img.save(&buffer, "PNG"); dbModify->dbinsertRecords(imageArray); }
qml side:
Page { property alias url: screenshoot.source //set source from another place Image { id: screenshoot } Button { id: button onClicked: { imgProvider.saveToDatabase(screenshoot) } } }
-
Hi,
You don't check that item is a valid pointer.
-
@SGaist said in How to Send QML image to C++ side:
item is a valid pointer
You mean this?
void ImageProvider::saveToDatabase(QObject *imageObj) { QQuickItemGrabResult *item = nullptr; item = qobject_cast<QQuickItemGrabResult *>(imageObj); if (item) { QImage img(item->image()); QByteArray imageArray; QBuffer buffer(&imageArray); buffer.open(QIODevice::WriteOnly); img.save(&buffer, "PNG"); dbModify->dbinsertRecords(imageArray); } }
The program does not crash, but nothing is stored in the database, the way that I send image to c++ is true?