Quantcast
Channel: Jobs
Viewing all articles
Browse latest Browse all 18427

Save image from qml

$
0
0
I am using snippet from this link http://qt-project.org/forums/viewthread/3948 It works very well in Qt 4.7 . Now I am using Qt 5.0.1 . I changed qdeclarative view to qquick view no other changes are made. I am getting Item is null from qdebug(image.cpp). Here is the code. what is error pls image.h #ifndef IMAGE_H #define IMAGE_H   #include <QtWidgets> #include<QGraphicsObject> #include<QGraphicsObject> #include<QObject>       class mainwindow:public  QWidget {     Q_OBJECT public:       mainwindow(QWidget *parent=0);     Q_INVOKABLE void save(QObject *item, const QString &path);   }; #endif // IMAGE_H image.cpp #include"image.h" #include<QtCore> #include<QGraphicsObject> #include<QImage> #include<QPainter>   mainwindow::mainwindow(QWidget *parent):QWidget(parent)   {   }     void mainwindow::save(QObject *imageObj, const QString &path) {       QGraphicsObject *item=qobject_cast<QGraphicsObject*>(imageObj);       if (!item) {         qDebug() << "Item is NULL";         return;     }       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);     img.save(path); } main.cpp #include"image.h" #include <QGuiApplication> #include <QQuickView> #include <QQmlEngine> #include <QApplication> #include <QQuickView> #include <QQmlContext>     int main(int argc,char *argv[]) {     QApplication app(argc,argv);       mainwindow window;      QQuickView view;     view.rootContext()->setContextProperty("myObject", &window);       view.setSource(QUrl::fromLocalFile("Test.qml"));     view.showMaximized();       return app.exec();   } Test.qml import QtQuick 2.0   Rectangle {     width: 640     height: 480     color: "white"       Image {         id: imgItem         fillMode: Image.PreserveAspectFit         source: "/home/bts-007/Desktop/test1.jpg"     }       MouseArea {         anchors.fill: parent         onClicked: {             myObject.save(imgItem, "/home/bts-007/Desktop/test_duplicate.jpg");         }     } } One more change I have to mention. In Test.qml it was import Qt 4.7 (changed to import QtQuick 2.0)

Viewing all articles
Browse latest Browse all 18427

Trending Articles