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:

Qt Code:
  1. #include <QtWidgets>
  2. #include "imageviewer.h"
  3.  
  4. ImageViewer::ImageViewer()
  5. {
  6. imageLabel = new QLabel;
  7. imageLabel->setBackgroundRole(QPalette::Base);
  8. imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  9. imageLabel->setScaledContents(true);
  10. setCentralWidget(imageLabel);
  11.  
  12. createActions();
  13. createMenus();
  14.  
  15. resize(570,357);
  16. }
  17.  
  18.  
  19. bool ImageViewer::loadFile(const QString &fileName)
  20. {
  21. QImageReader reader(fileName);
  22. const QImage image = reader.read();
  23. if (image.isNull()) {
  24. QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
  25. tr("Cannot load %1.").arg(QDir::toNativeSeparators(fileName)));
  26. setWindowFilePath(QString());
  27. imageLabel->setPixmap(QPixmap());
  28. imageLabel->adjustSize();
  29. return false;
  30. }
  31. imageLabel->setPixmap(QPixmap::fromImage(image).scaled(size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
  32. return true;
  33. }
  34.  
  35.  
  36.  
  37. void ImageViewer::open()
  38. {
  39. QStringList mimeTypeFilters;
  40. foreach (const QByteArray &mimeTypeName, QImageReader::supportedMimeTypes())
  41. mimeTypeFilters.append(mimeTypeName);
  42. mimeTypeFilters.sort();
  43. const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
  44. QFileDialog dialog(this, tr("Open File"),
  45. picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.last());
  46. dialog.setAcceptMode(QFileDialog::AcceptOpen);
  47. dialog.setMimeTypeFilters(mimeTypeFilters);
  48. dialog.selectMimeTypeFilter("image/jpeg");
  49.  
  50. while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
  51. }
  52.  
  53. void ImageViewer::createActions()
  54. {
  55. openAct = new QAction(tr("&Open..."), this);
  56. openAct->setShortcut(tr("Ctrl+O"));
  57. connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
  58.  
  59. }
  60.  
  61. void ImageViewer::createMenus()
  62. {
  63. fileMenu = new QMenu(tr("&File"), this);
  64. fileMenu->addAction(openAct);
  65.  
  66. menuBar()->addMenu(fileMenu);
  67.  
  68. }
To copy to clipboard, switch view to plain text mode 

Header file:

Qt Code:
  1. #ifndef IMAGEVIEWER_H
  2. #define IMAGEVIEWER_H
  3.  
  4. #include <QMainWindow>
  5.  
  6. class QAction;
  7. class QLabel;
  8. class QMenu;
  9. class QScrollBar;
  10.  
  11. class ImageViewer : public QMainWindow
  12. {
  13. Q_OBJECT
  14.  
  15. public:
  16. ImageViewer();
  17. bool loadFile(const QString &);
  18.  
  19. private slots:
  20. void open();
  21.  
  22. private:
  23. void createActions();
  24. void createMenus();
  25.  
  26. QLabel *imageLabel;
  27. QAction *openAct;
  28. QMenu *fileMenu;
  29. };
  30.  
  31. #endif
To copy to clipboard, switch view to plain text mode 

Main.cpp:

Qt Code:
  1. #include <QApplication>
  2. #include <QCommandLineParser>
  3.  
  4. #include "imageviewer.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QApplication app(argc, argv);
  9. QGuiApplication::setApplicationDisplayName(ImageViewer::tr("Image Viewer"));
  10. ImageViewer imageViewer;
  11. imageViewer.show();
  12. return app.exec();
  13. }
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?