Results 1 to 6 of 6

Thread: Problem with QPixmap

  1. #1

    Default Problem with QPixmap

    Hi

    I'm trying to load an image into a QPixmap object, here is my code:


    QPixmap blackKingPixmap;
    std::cout << "Hello";
    blackKingPixmap.load("images/black_king.png");
    std::cout << "Hello";

    When I run it, I only see one 'Hello' printed to the console, shortly after that the program terminates without printing the second 'Hello'.
    I don't get any error messages when I run it, it simply just terminates. I have tried putting the 'images' folder in the 'resources' folder, and in the same folder as the .cpp file. I have tried taking the images out of the folder and putting them directly in the 'resources' folder. I have tried specifying the absolute path to the image ('C:<user>/ etcetc'). I even tried loading the image to a QImage first and then trying to create the QPixmap from the QImage. I have checked that the image definitely exists at the specified path, and that the permissions for accessing the image are correct. All of which to no avail! Basically I have tried everything that I (or ChatGPT) could think of to fix it, none of it worked.

    Can someone please help me? (Or put me out of my misery haha)

    Thanks a lot,
    Michael

  2. #2

    Default Re: Problem with QPixmap

    You need to set a container for your QPixmap. I do as follows and maybe you can try
    Qt Code:
    1. ui->label_picRange->setVisible(true); //QLabel for image was defined in mainwindow.ui
    2. picRange = QPixmap(":/img/picRange.jpg"); //QPixmap picRange was declared in the header
    3. if (picRange.isNull()) {
    4. qDebug() << "Failed to load picRange.jpg";
    5. } else {
    6. //putting the image into the label
    7. ui->label_picRange->setPixmap(picRange.scaled(ui->label_picRange->width(), ui->label_picRange->height(), Qt::KeepAspectRatio));
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by zvoopz; 6th November 2024 at 09:24.

  3. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    517
    Thanks
    12
    Thanked 77 Times in 75 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QPixmap

    Hi, I think you should use a debugger to see what's going on there. Maybe check if the pixmap file itself is corrupted? Can you open it with other tools?

    Ginsengelf

  4. #4

    Default Re: Problem with QPixmap

    Always make sure to use the full path or double-check the path relative to the working directory. Use functions like QFileInfo::exists() to check the file before downloading.

  5. #5
    Join Date
    Nov 2024
    Posts
    7
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: Problem with QPixmap

    Your program crashes because QPixmap requires a QApplication object to initialize the GUI system. Add `QApplication app(argc, argv);` at the start of your `main()` function to resolve this. Verify the image path is correct relative to the working directory, or test with an absolute path, and ensure the image format (e.g., PNG) is supported.
    Slope Game

  6. #6

    Default Re: Problem with QPixmap

    It's likely due to a missing resource or an issue with the image file itself. Make sure the path to "images/black_king.png" is correct relative to your executable's working directory, and check if the image format is supported by Qt. You can add error handling by checking if blackKingPixmap.load() returns false, which will help identify if the loading failed. Additionally, consider using QImage for debugging, as it may provide more informative error messages.
    slope game
    Last edited by edmiller; 13th January 2025 at 10:02.

Similar Threads

  1. QPixmap loadFromData() problem..
    By zgulser in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2012, 08:47
  2. QPixmap problem.
    By muny in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2009, 00:40
  3. Zoom in Problem for QPixmap
    By jsmith in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2009, 11:27
  4. QPixmap::grabWindow() problem
    By mtroscheck in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2009, 01:15
  5. QPixmap resize problem
    By khcbabu in forum Qt Programming
    Replies: 2
    Last Post: 17th November 2008, 14:29

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
  •  
Qt is a trademark of The Qt Company.