Search code examples
c++qtnullqpixmap

QPixmap does not open some image


It opens one JPEG image but not the other JPEG. The directories and files exist on my system. For ease of recreation the images used in the below code are console test and f2.

#include "mainwindow.h"
#include <QApplication>
#include <QPixmap>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    QPixmap *pixmap = new QPixmap;

    pixmap->load("C:/Programs/console test.jpg");
    if(pixmap->isNull())
        cout << "darn" << endl;
    else
        cout << "not null" << endl;

    pixmap->load("C:/Programs/f2.jpg");

    if(pixmap->isNull())
        cout << "darn" << endl;
    else
        cout << "not null" << endl;

    return a.exec();
}

The above code prints

darn
not null

If relevant, the application is a QWidget application.


Solution

  • console test.jpg is actually a PNG file. – Oktalist

    Oktalist answered the question. I naively used and changed the extension from png to jpg expecting file conversion. After undoing the change with the same method, the image successfully opens.

    ifstream src("console print.jpg", ios::binary);
    ofstream dest("console print.png", ios::binary);
    dest << src.rdbuf();
    dest.flush();
    

    And now console print has the correct file extension. And it opens. There are a billion other ways you could do this, but this one was easy.

    -Sanfer