Save image from qml
-
wrote on 6 Jun 2013, 13:07 last edited by
Hi,
-take a look at "this thread":http://qt-project.org/forums/viewthread/3948.-
My apologies, did not see that you mentioned this same topic. Will post more after taking another look at the code.
-
wrote on 7 Jun 2013, 06:53 last edited by
Alek smierciak. I followed the link suggested by you
http://qt-project.org/doc/qt-5.0/qtquick/qtquick-porting-qt5.htmlI changed the profile to
@
QT += quick1
@getting an error
Project ERROR: Unknown module(s) in QT: quick1 -
wrote on 7 Jun 2013, 09:18 last edited by
Seems there was a rollback, because my post was reverted to its previous edit somewhere after your response. Oh well, no matter.
You would've be able to test it in a fallback QtQuick 1.0 mode provided you had this module installed - you probably don't, looking at the error.
-
wrote on 7 Jun 2013, 09:34 last edited by
How can i install this module in qt 5.0.1
-
wrote on 7 Jun 2013, 09:51 last edited by
Alek Smierciak when I was searching ,I came across this will it be using for me
https://launchpad.net/ubuntu/+source/qtquick1-opensource-src/5.0.1-0ubuntu1
-
wrote on 7 Jun 2013, 10:11 last edited by
The source code for qtquick1 module is hosted "here":http://qt.gitorious.org/qt/qtquick1, but the code should be included in the installation bundle.
How did you install your Qt 5 library?
-
wrote on 7 Jun 2013, 10:19 last edited by
I download it from Qt
qt-linux-opensource-5.0.1-x86-offline.run
and installed it. I dont know about libraries.
-
wrote on 7 Jun 2013, 10:34 last edited by
When installing Qt you need to specify which modules you want. QtQuick1 module should be on the list. Try reinstalling Qt, installing in another directory or "installing the module":http://qt-project.org/forums/viewthread/27179 through building the source "from Git":http://qt.gitorious.org/qt/qtquick1.
-
wrote on 7 Jun 2013, 13:41 last edited by
I dont know how to proceed. I am planning to blend two images and then save. Qtgraphics element is present qt 5 and image saver is working only in qt 4. help
-
wrote on 7 Jun 2013, 13:53 last edited by
Another approach would be to use the "Canvas":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-canvas.html element, load two images and "save them as a file":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-canvas.html#save-method.
-
wrote on 7 Jun 2013, 14:09 last edited by
Alek smierciak thank you. I tried an approach which is not working. here is the code
@
import QtQuick 2.0Rectangle {
width: 640
height: 480
color: "white"Image { id: imgItem fillMode: Image.PreserveAspectFit source: "/home/bts-004/Desktop/test1.jpg" } MouseArea { anchors.fill: parent onClicked: { canobj.loadImage("/home/bts-004/Desktop/test1.jpg") canobj.save("/home/bts-004/Desktop/test_duplicate.jpg") } } Canvas{ id:canobj }
}
@
Is there any thing I have to add. -
wrote on 7 Jun 2013, 15:02 last edited by
bts-007, please read the documentation part for "Canvas::loadImage":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-canvas.html#loadImage-method carefully. It loads an image asynchronously, meaning that a signal will be emitted on completing - in this case, a Canvas::imageLoaded() signal with "Canvas::onImageLoaded()":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-canvas.html#onImageLoaded-signal signal handler.
You need to put the Canvas::save function call to the signal handler code part.
-
wrote on 9 Jun 2013, 08:04 last edited by
I have tried this but not saving
@
import QtQuick 2.0Rectangle {
width: 640
height: 480
color: "white"Image { id: imgItem fillMode: Image.PreserveAspectFit source: "/home/bts-004/Desktop/test1.jpg" } MouseArea { anchors.fill: parent onClicked: { canobj.loadImage("/home/bts-004/Desktop/test1.jpg") } } Canvas{ id:canobj onImageLoaded: save("/home/bts-004/Desktop/test_duplicate.jpg") }
}
@
-
wrote on 9 Jun 2013, 08:34 last edited by
Check if the onImageLoaded signal handler is actually called. Perhaps there is a problem with loading test1.jpg? Also, explain what do you mean by "not saving". Is the file created at all or not? Do you get any errors or warning in the Application output?
-
wrote on 9 Jun 2013, 09:37 last edited by
To check that I tried something like this
@
import QtQuick 2.0Rectangle {
width: 640
height: 480
color: "white"Image { id: imgItem fillMode: Image.PreserveAspectFit } MouseArea { anchors.fill: parent onClicked: { imgItem.source= "/home/bts-004/Desktop/test1.jpg" canobj.loadImage("/home/bts-004/Desktop/test1.jpg") } } Canvas{ id:canobj onImageLoaded: save("/home/bts-004/Desktop/test_duplicate.jpg") }
}
@on clicking ,image was loaded on the screen. there is no action from canvas side. No errors and when I check the desktop there is no file (test_duplicate.jpg)
-
wrote on 9 Jun 2013, 10:21 last edited by
By saying "image was loaded on the screen", do you mean on the Image element or the Canvas element?
Please also change:
@ onImageLoaded: save("/home/bts-004/Desktop/test_duplicate.jpg")@
to:
@ onImageLoaded: {
console.log("onImageLoaded was called")
console.log(canobj.isImageLoaded("/home/bts-004/Desktop/test1.jpg")
if (save("/home/bts-004/Desktop/test_duplicate.jpg")) {
console.log("Canvas::save() executed")
}
}@and tell me do you get any console output out of that?
-
wrote on 9 Jun 2013, 10:54 last edited by
Yeah Image was loaded on image element. I tried your code. no console messages were displayed.
-
wrote on 12 Jun 2013, 07:19 last edited by
I have the same problem too, do we have to design a save able image type by ourselves?Inherit QQuickPaintedItem, draw the image by QPainter and save it when we need?
I will put the codes onto this website after I finish it(if it can work)
-
wrote on 12 Jun 2013, 10:37 last edited by
I try my best to write a saveAbleImageType, a naive implementation, still need to sharpen it.
.hpp
@
#ifndef SAVEABLEIMAGE3_HPP
#define SAVEABLEIMAGE3_HPP#include <QImage>
#include <QQuickPaintedItem>
#include <QString>class QPainter;
class QQuickItem;class saveAbleImage : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)
Q_PROPERTY(bool smooth READ smooth WRITE setSmooth NOTIFY smoothChanged)
Q_PROPERTY(int height READ height WRITE setHeight NOTIFY heightChanged)
Q_PROPERTY(int width READ width WRITE setWidth NOTIFY widthChanged)public:
saveAbleImage(QQuickItem *parent = 0);
saveAbleImage(saveAbleImage const&) = delete;
saveAbleImage& operator=(saveAbleImage const&) = delete;virtual void paint(QPainter *painter); //properties of READ int height() const; bool smooth() const; QString source() const; int width() const; //properties of READ //properties of WRITE void setHeight(int height); void setSource(QString const &source); void setSmooth(bool smooth); void setWidth(int width); //properties of WRITE
public slots:
void save(QString const &path) const;signals:
void heightChanged();void smoothChanged(); void sourceChanged(); void widthChanged();
private:
int height_;QImage image_; QImage imageBuffer_; bool smooth_; QString source_; int width_;
};
#endif // SAVEABLEIMAGE3_HPP
@
.cpp
@
#include "saveAbleImage3.hpp"#include <QPainter>
#include <QQuickItem>saveAbleImage::saveAbleImage(QQuickItem *parent) : QQuickPaintedItem(parent),
height_(0),
smooth_(false),
width_(0)
{}
void saveAbleImage::paint(QPainter *painter)
{
qDebug() <<"paint : " <<width_ <<", "<<height_;
if(height_ != 0 && width_ != 0){
qDebug() <<"scale paint : " << width_<<", "<<height_;
if(smooth_){
imageBuffer_ = image_.scaled(QSize(width_, height_), Qt::KeepAspectRatio, Qt::SmoothTransformation);
qDebug() <<"scale size : " <<image_.width() <<", "<<image_.height();
}else{
imageBuffer_ = image_.scaled(QSize(width_, height_), Qt::KeepAspectRatio, Qt::FastTransformation);
qDebug() <<"scale size : " <<image_.width() <<", "<<image_.height();
}
painter->drawImage((width_ - imageBuffer_.width()) / 2, (height_ - imageBuffer_.height()) / 2, imageBuffer_);
}else{
painter->drawImage(0, 0, image_);
}
}int saveAbleImage::height() const
{
return height_;
}void saveAbleImage::save(QString const &path) const
{
qDebug()<< "save image";
if(!imageBuffer_.isNull()){
qDebug()<<"save image : "<<imageBuffer_.save(path);
}else{
qDebug()<<"save image : "<<image_.save(path);
}
}bool saveAbleImage::smooth() const
{
return smooth_;
}QString saveAbleImage::source() const
{
return source_;
}int saveAbleImage::width() const
{
return width_;
}void saveAbleImage::saveAbleImage::setHeight(int height)
{
qDebug() <<"height : "<< height;
if(height_ != height){
height_ = height;emit heightChanged(); }
}
void saveAbleImage::setSource(QString const &source)
{
qDebug() << "source = "<<source;
if(source_ != source){
source_ = source;image_.load(source_); update(); emit sourceChanged(); }
}
void saveAbleImage::setSmooth(bool smooth)
{
if(smooth_ != smooth){
smooth_ = smooth;emit smoothChanged(); }
}
void saveAbleImage::setWidth(int width)
{
qDebug() <<"width : "<< width;
if(width_ != width){
width_ = width;emit widthChanged(); }
}
@use it like this
@
SaveAbleImage{
id: buganchors.fill: parent source: "Pictures/1369996733291.jpg" height:root.height width: root.width MouseArea{ anchors.fill: bug onClicked: { bug.save("Pictures/saveTest.jpg") } } }
@
I wonder why the QtQuick Image do not provide a function for us to save the image?
-
wrote on 13 Jun 2013, 16:36 last edited by
I tried Canvas, can't work either
If you just need to load and save the image
here is a simple class for that.hpp
@
#include <QImage>
#include <QObject>class QQuickItem;
class QString;class imageSaver : public QObject
{
Q_OBJECT
public:
explicit imageSaver(QObject *parent = 0);
imageSaver(imageSaver const&) = delete;
imageSaver& operator=(imageSaver const&) = delete;public slots:
void load(QString const &path);
void loadAndSave(QString const &path);
void save(QString const &path) const;private:
QImage img_;
};
@.cpp
@
#include "imageSaver.hpp"imageSaver::imageSaver(QObject *parent) :
QObject(parent)
{}
void imageSaver::load(QString const &path)
{
img_.load(path);
}void imageSaver::loadAndSave(QString const &path)
{
img_.load(path);
img_.save(path);
}void imageSaver::save(const QString &path) const
{
img_.save(path);
}
@if you need to take the result after postprocessing, take the screenshot
"take screenshot":http://qt-project.org/forums/viewthread/28822/With these, we can keep on using the Image from qml2
no error handle, please refine it by yourself
11/21