Results 1 to 4 of 4

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
    516
    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.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.