Results 1 to 4 of 4

Thread: QImage doesn't seem to load any images at all (always returning null)?

  1. #1
    Join Date
    Apr 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QImage doesn't seem to load any images at all (always returning null)?

    I'm on Windows7 and using Qt SDK 4.8.
    Trying to read a file in with QImage but it just doesn't seem to load. That is, QImage(filename) or QImage(filename, "PNG") or QImage.load(filename) always return NULL.

    Here's my code:

    Qt Code:
    1. QString fileName = QFileDialog::getOpenFileName(this,
    2. tr("Open Image"),
    3. QDir::homePath(),
    4. tr("Image Files (*.png *.tga *.bmp)"));
    5.  
    6. if (!fileName.isEmpty())
    7. {
    8. targetImage = new QImage(fileName, "PNG");
    9. if(targetImage->isNull())
    10. {
    11. QMessageBox::information(this,
    12. tr("Viewer"),
    13. tr("Cannot load %1.").arg(fileName));
    14. return;
    15. }
    16.  
    17. onScreenImage.setBackgroundRole(QPalette::Base);
    18. onScreenImage.setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    19. onScreenImage.setScaledContents(true);
    20. onScreenImage.setPixmap(QPixmap::fromImage(*targetImage));
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong?

  2. #2
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QImage doesn't seem to load any images at all (always returning null)?

    Qt Code:
    1. targetImage = new QImage(fileName, "PNG");
    To copy to clipboard, switch view to plain text mode 

    Remove the optional format parameter ("PNG") and just use the default so QImage will autodetect the file type otherwise you need to filter QFileDialog only to png files

    Qt Code:
    1. QString fileName = QFileDialog::getOpenFileName(...
    To copy to clipboard, switch view to plain text mode 

    What is the value of fileName here? For QImage you will need the full path not just the fileName.

  3. #3
    Join Date
    Jan 2008
    Posts
    39
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage doesn't seem to load any images at all (always returning null)?

    onScreenImage.setPixmap(QPixmap::fromImage(*target Image));
    i think you have to cahnge this. hope it helps
    onScreenImage.setPixmap(QPixmap::fromImage(targetI mage));

  4. #4
    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: QImage doesn't seem to load any images at all (always returning null)?

    Comments embedded:
    Quote Originally Posted by SaldaVonSchwartz View Post
    Qt Code:
    1. QString fileName = QFileDialog::getOpenFileName(this,
    2. tr("Open Image"),
    3. QDir::homePath(),
    4. tr("Image Files (*.png *.tga *.bmp)"));
    5. // you could have a BMP or Targa file at this point
    6. // Qt has no Targa format support out of the box
    7. if (!fileName.isEmpty())
    8. {
    9. targetImage = new QImage(fileName, "PNG");
    10. // Not much good if the file is not a PNG
    11. // You allocate memory you do not free: memory leak
    12. // No need for this to be on the heap at all
    13.  
    14. if(targetImage->isNull())
    15. {
    16. QMessageBox::information(this,
    17. tr("Viewer"),
    18. tr("Cannot load %1.").arg(fileName));
    19. return;
    20. }
    21.  
    22. onScreenImage.setBackgroundRole(QPalette::Base);
    23. onScreenImage.setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    24. onScreenImage.setScaledContents(true);
    25. onScreenImage.setPixmap(QPixmap::fromImage(*targetImage));
    26. // Why did you use a QImage if you require a QPixmap ultimately?
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 
    You don't say if it is failing in the development or deployed environment. None of this will work deployed unless you also deploy the necessary imageformat plugins. What does QImageReader::supportedImageFormats() return?

Similar Threads

  1. [Qt4] How to load Url image into QImage?
    By Gonzalez in forum Qt Programming
    Replies: 6
    Last Post: 13th October 2014, 10:10
  2. Replies: 1
    Last Post: 16th November 2010, 17:45
  3. Replies: 4
    Last Post: 27th July 2009, 15:45
  4. Statically linked program doesn't load QImage from file.
    By badjer1024 in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2009, 18:45

Tags for this Thread

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.