h.a.n.d
15th May 2016, 12:23
Hi,
I'm trying to convert the "imagescaling" example http://doc.qt.io/qt-5/qtconcurrent-imagescaling-example.html to work with a call to a member function instead of the global one.
Here is the modified code:
imagescaling.cpp
#include "imagescaling.h"
#include <qmath.h>
const int imageSize = 100;
QImage* scale(const QString &imageFileName)
{
QImage* image = new QImage(imageFileName);
image->scaled(QSize(imageSize, imageSize), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
return image;
}
Images::Images(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("Image loading and scaling example"));
resize(800, 600);
imageScaling = new QFutureWatcher<QImage*>(this);
connect(imageScaling, SIGNAL(resultReadyAt(int)), SLOT(showImage(int)));
connect(imageScaling, SIGNAL(finished()), SLOT(finished()));
openButton = new QPushButton(tr("Open Images"));
connect(openButton, SIGNAL(clicked()), SLOT(open()));
cancelButton = new QPushButton(tr("Cancel"));
cancelButton->setEnabled(false);
connect(cancelButton, SIGNAL(clicked()), imageScaling, SLOT(cancel()));
pauseButton = new QPushButton(tr("Pause/Resume"));
pauseButton->setEnabled(false);
connect(pauseButton, SIGNAL(clicked()), imageScaling, SLOT(togglePaused()));
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addWidget(openButton);
buttonLayout->addWidget(cancelButton);
buttonLayout->addWidget(pauseButton);
buttonLayout->addStretch();
imagesLayout = new QGridLayout();
mainLayout = new QVBoxLayout();
mainLayout->addLayout(buttonLayout);
mainLayout->addLayout(imagesLayout);
mainLayout->addStretch();
setLayout(mainLayout);
}
Images::~Images()
{
imageScaling->cancel();
imageScaling->waitForFinished();
}
QImage* Images::scale_(const QString &imageFileName)
{
QImage* image = new QImage(imageFileName);
image->scaled(QSize(imageSize, imageSize), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
return image;
}
void Images::open()
{
// Cancel and wait if we are already loading images.
if (imageScaling->isRunning()) {
imageScaling->cancel();
imageScaling->waitForFinished();
}
// Show a file open dialog at QStandardPaths::PicturesLocation.
QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Images"),
QStandardPaths::writableLocation(QStandardPaths::P icturesLocation),
"*.jpg *.png");
if (files.count() == 0)
return;
// Do a simple layout.
qDeleteAll(labels);
labels.clear();
int dim = qSqrt(qreal(files.count())) + 1;
for (int i = 0; i < dim; ++i) {
for (int j = 0; j < dim; ++j) {
QLabel *imageLabel = new QLabel;
imageLabel->setFixedSize(imageSize,imageSize);
imagesLayout->addWidget(imageLabel,i,j);
labels.append(imageLabel);
}
}
// Use mapped to run the thread safe scale function on the files.
//imageScaling->setFuture(QtConcurrent::mapped(files, scale));
imageScaling->setFuture(QtConcurrent::mapped(files, [this](const QString &file){this->scale_(file);}));
openButton->setEnabled(false);
cancelButton->setEnabled(true);
pauseButton->setEnabled(true);
}
void Images::showImage(int num)
{
labels[num]->setPixmap(QPixmap::fromImage(*(imageScaling->resultAt(num))));
}
void Images::finished()
{
openButton->setEnabled(true);
cancelButton->setEnabled(false);
pauseButton->setEnabled(false);
}
imagescaling.h
#ifndef IMAGESCALING_H
#define IMAGESCALING_H
#include <QtWidgets>
#include <QtConcurrent>
class Images : public QWidget
{
Q_OBJECT
public:
Images(QWidget *parent = 0);
~Images();
QImage *scale_(const QString &imageFileName);
public Q_SLOTS:
void open();
void showImage(int num);
void finished();
private:
QPushButton *openButton;
QPushButton *cancelButton;
QPushButton *pauseButton;
QVBoxLayout *mainLayout;
QList<QLabel *> labels;
QGridLayout *imagesLayout;
QFutureWatcher<QImage*> *imageScaling;
};
#endif // IMAGESCALING_H
If I map the global function everything works:
imageScaling->setFuture(QtConcurrent::mapped(files, scale));
If I map to the member function it won't compile:
imageScaling->setFuture(QtConcurrent::mapped(files, &Images::scale_));
or
imageScaling->setFuture(QtConcurrent::mapped(files, [this](const QString &file){this->scale_(file);}));
ERROR:
...\imagescaling.cpp:133: Fehler: C2664: "void QFutureWatcher<QImage *>::setFuture(const QFuture<T> &)" : Converting of Argument 1 of "QFuture<void>" in "const QFuture<T> &" not possible
with
[
T=QImage *
]
Is using QtConcurrent::mapped() the wrong approach to bind with member functions?
I'm trying to convert the "imagescaling" example http://doc.qt.io/qt-5/qtconcurrent-imagescaling-example.html to work with a call to a member function instead of the global one.
Here is the modified code:
imagescaling.cpp
#include "imagescaling.h"
#include <qmath.h>
const int imageSize = 100;
QImage* scale(const QString &imageFileName)
{
QImage* image = new QImage(imageFileName);
image->scaled(QSize(imageSize, imageSize), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
return image;
}
Images::Images(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("Image loading and scaling example"));
resize(800, 600);
imageScaling = new QFutureWatcher<QImage*>(this);
connect(imageScaling, SIGNAL(resultReadyAt(int)), SLOT(showImage(int)));
connect(imageScaling, SIGNAL(finished()), SLOT(finished()));
openButton = new QPushButton(tr("Open Images"));
connect(openButton, SIGNAL(clicked()), SLOT(open()));
cancelButton = new QPushButton(tr("Cancel"));
cancelButton->setEnabled(false);
connect(cancelButton, SIGNAL(clicked()), imageScaling, SLOT(cancel()));
pauseButton = new QPushButton(tr("Pause/Resume"));
pauseButton->setEnabled(false);
connect(pauseButton, SIGNAL(clicked()), imageScaling, SLOT(togglePaused()));
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addWidget(openButton);
buttonLayout->addWidget(cancelButton);
buttonLayout->addWidget(pauseButton);
buttonLayout->addStretch();
imagesLayout = new QGridLayout();
mainLayout = new QVBoxLayout();
mainLayout->addLayout(buttonLayout);
mainLayout->addLayout(imagesLayout);
mainLayout->addStretch();
setLayout(mainLayout);
}
Images::~Images()
{
imageScaling->cancel();
imageScaling->waitForFinished();
}
QImage* Images::scale_(const QString &imageFileName)
{
QImage* image = new QImage(imageFileName);
image->scaled(QSize(imageSize, imageSize), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
return image;
}
void Images::open()
{
// Cancel and wait if we are already loading images.
if (imageScaling->isRunning()) {
imageScaling->cancel();
imageScaling->waitForFinished();
}
// Show a file open dialog at QStandardPaths::PicturesLocation.
QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Images"),
QStandardPaths::writableLocation(QStandardPaths::P icturesLocation),
"*.jpg *.png");
if (files.count() == 0)
return;
// Do a simple layout.
qDeleteAll(labels);
labels.clear();
int dim = qSqrt(qreal(files.count())) + 1;
for (int i = 0; i < dim; ++i) {
for (int j = 0; j < dim; ++j) {
QLabel *imageLabel = new QLabel;
imageLabel->setFixedSize(imageSize,imageSize);
imagesLayout->addWidget(imageLabel,i,j);
labels.append(imageLabel);
}
}
// Use mapped to run the thread safe scale function on the files.
//imageScaling->setFuture(QtConcurrent::mapped(files, scale));
imageScaling->setFuture(QtConcurrent::mapped(files, [this](const QString &file){this->scale_(file);}));
openButton->setEnabled(false);
cancelButton->setEnabled(true);
pauseButton->setEnabled(true);
}
void Images::showImage(int num)
{
labels[num]->setPixmap(QPixmap::fromImage(*(imageScaling->resultAt(num))));
}
void Images::finished()
{
openButton->setEnabled(true);
cancelButton->setEnabled(false);
pauseButton->setEnabled(false);
}
imagescaling.h
#ifndef IMAGESCALING_H
#define IMAGESCALING_H
#include <QtWidgets>
#include <QtConcurrent>
class Images : public QWidget
{
Q_OBJECT
public:
Images(QWidget *parent = 0);
~Images();
QImage *scale_(const QString &imageFileName);
public Q_SLOTS:
void open();
void showImage(int num);
void finished();
private:
QPushButton *openButton;
QPushButton *cancelButton;
QPushButton *pauseButton;
QVBoxLayout *mainLayout;
QList<QLabel *> labels;
QGridLayout *imagesLayout;
QFutureWatcher<QImage*> *imageScaling;
};
#endif // IMAGESCALING_H
If I map the global function everything works:
imageScaling->setFuture(QtConcurrent::mapped(files, scale));
If I map to the member function it won't compile:
imageScaling->setFuture(QtConcurrent::mapped(files, &Images::scale_));
or
imageScaling->setFuture(QtConcurrent::mapped(files, [this](const QString &file){this->scale_(file);}));
ERROR:
...\imagescaling.cpp:133: Fehler: C2664: "void QFutureWatcher<QImage *>::setFuture(const QFuture<T> &)" : Converting of Argument 1 of "QFuture<void>" in "const QFuture<T> &" not possible
with
[
T=QImage *
]
Is using QtConcurrent::mapped() the wrong approach to bind with member functions?