Results 1 to 4 of 4

Thread: Image resolution lowered on setting background of a QLabel through QPixmap

  1. #1
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Image resolution lowered on setting background of a QLabel through QPixmap

    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?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Image resolution lowered on setting background of a QLabel through QPixmap

    Firstly, you are resizing the image to the size() of the QMainWindow rather than the label you will be putting the pixmap into. Secondly, you are telling Qt to ignore the aspect ratio when resizing so it probably Will stretch/shrink the image in one direction to fit the size you gave it.

  3. #3
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Image resolution lowered on setting background of a QLabel through QPixmap

    Changed to imageLabel->setPixmap(QPixmap::fromImage(image).scaled(QSize( 570,377),Qt::KeepAspectRatio,Qt::SmoothTransformat ion)); Same result. I have already tried it, I was just experimenting with various options and put this version in the question.

  4. #4
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Image resolution lowered on setting background of a QLabel through QPixmap

    Changed to a solution where I am caching a pixmap variable and updating it. That solves the problem. It is a known Qt bug on Retina Display Mac.

Similar Threads

  1. Adding image to QLabel using QPixmap
    By jvalerio in forum Newbie
    Replies: 1
    Last Post: 15th December 2014, 17:33
  2. Problems setting a background image
    By franco.amato in forum Qt Programming
    Replies: 10
    Last Post: 28th January 2011, 22:33
  3. setting image to QLabel in new thread
    By troorl in forum Qt Programming
    Replies: 2
    Last Post: 3rd September 2008, 08:20
  4. background-image on QLabel does NOT work
    By VireX in forum Newbie
    Replies: 2
    Last Post: 8th June 2007, 20:30
  5. Setting background image of QFrame
    By Claymore in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2007, 19:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.