Hi, I have a QLabel where I need to fit an image. If I load the image using painter and then update the label, it comes up fine. However, I will need to zoom in/out the image (that I try to do by calling resize() on the QLabel) on click (zoom the image inline, not load a zoomed version of the same image!), which this method is not allowing me to do so. So I decided to do it through QPixmap. This is my code (kind of copied from Qt official ImageViewer example here:
#include <QtWidgets>
#include "imageviewer.h"
ImageViewer::ImageViewer()
{
imageLabel
->setBackgroundRole
(QPalette::Base);
imageLabel->setScaledContents(true);
setCentralWidget(imageLabel);
createActions();
createMenus();
resize(570,357);
}
bool ImageViewer
::loadFile(const QString &fileName
) {
const QImage image
= reader.
read();
if (image.isNull()) {
QMessageBox::information(this, QGuiApplication
::applicationDisplayName(),
tr
("Cannot load %1.").
arg(QDir::toNativeSeparators(fileName
)));
imageLabel->adjustSize();
return false;
}
imageLabel
->setPixmap
(QPixmap::fromImage(image
).
scaled(size
(),Qt
::IgnoreAspectRatio,Qt
::SmoothTransformation));
return true;
}
void ImageViewer::open()
{
mimeTypeFilters.append(mimeTypeName);
mimeTypeFilters.sort();
const QStringList picturesLocations
= QStandardPaths
::standardLocations(QStandardPaths
::PicturesLocation);
picturesLocations.
isEmpty() ?
QDir::currentPath() : picturesLocations.
last());
dialog.setMimeTypeFilters(mimeTypeFilters);
dialog.selectMimeTypeFilter("image/jpeg");
while (dialog.
exec() == QDialog::Accepted && !loadFile
(dialog.
selectedFiles().
first())) {} }
void ImageViewer::createActions()
{
openAct
= new QAction(tr
("&Open..."),
this);
openAct->setShortcut(tr("Ctrl+O"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
}
void ImageViewer::createMenus()
{
fileMenu
= new QMenu(tr
("&File"),
this);
fileMenu->addAction(openAct);
menuBar()->addMenu(fileMenu);
}
#include <QtWidgets>
#include "imageviewer.h"
ImageViewer::ImageViewer()
{
imageLabel = new QLabel;
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true);
setCentralWidget(imageLabel);
createActions();
createMenus();
resize(570,357);
}
bool ImageViewer::loadFile(const QString &fileName)
{
QImageReader reader(fileName);
const QImage image = reader.read();
if (image.isNull()) {
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr("Cannot load %1.").arg(QDir::toNativeSeparators(fileName)));
setWindowFilePath(QString());
imageLabel->setPixmap(QPixmap());
imageLabel->adjustSize();
return false;
}
imageLabel->setPixmap(QPixmap::fromImage(image).scaled(size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
return true;
}
void ImageViewer::open()
{
QStringList mimeTypeFilters;
foreach (const QByteArray &mimeTypeName, QImageReader::supportedMimeTypes())
mimeTypeFilters.append(mimeTypeName);
mimeTypeFilters.sort();
const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
QFileDialog dialog(this, tr("Open File"),
picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.last());
dialog.setAcceptMode(QFileDialog::AcceptOpen);
dialog.setMimeTypeFilters(mimeTypeFilters);
dialog.selectMimeTypeFilter("image/jpeg");
while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
}
void ImageViewer::createActions()
{
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcut(tr("Ctrl+O"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
}
void ImageViewer::createMenus()
{
fileMenu = new QMenu(tr("&File"), this);
fileMenu->addAction(openAct);
menuBar()->addMenu(fileMenu);
}
To copy to clipboard, switch view to plain text mode
Header file:
#ifndef IMAGEVIEWER_H
#define IMAGEVIEWER_H
#include <QMainWindow>
{
Q_OBJECT
public:
ImageViewer();
private slots:
void open();
private:
void createActions();
void createMenus();
};
#endif
#ifndef IMAGEVIEWER_H
#define IMAGEVIEWER_H
#include <QMainWindow>
class QAction;
class QLabel;
class QMenu;
class QScrollArea;
class QScrollBar;
class ImageViewer : public QMainWindow
{
Q_OBJECT
public:
ImageViewer();
bool loadFile(const QString &);
private slots:
void open();
private:
void createActions();
void createMenus();
QLabel *imageLabel;
QAction *openAct;
QMenu *fileMenu;
};
#endif
To copy to clipboard, switch view to plain text mode
Main.cpp:
#include <QApplication>
#include <QCommandLineParser>
#include "imageviewer.h"
int main(int argc, char *argv[])
{
QGuiApplication::setApplicationDisplayName(ImageViewer::tr("Image Viewer"));
ImageViewer imageViewer;
imageViewer.show();
return app.exec();
}
#include <QApplication>
#include <QCommandLineParser>
#include "imageviewer.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGuiApplication::setApplicationDisplayName(ImageViewer::tr("Image Viewer"));
ImageViewer imageViewer;
imageViewer.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
This is the image I am uploading to the app: link. As you can see, the viewport size here is 570 by 357. The image I am using is 2560 by 1600 (around 16 MB size), both have an aspect ratio of almost 1.6. However, on loading the image, it comes as rather blurred:
TH9uC.jpg
How do I make it look sharp as the original image, just as seen when using QPainter?
Bookmarks