Newbie problem with QT C ++ - Qimage not working?

I am trying to make a console application for reading pixels from an image:

#include <QtCore/QCoreApplication> #include <QtGui/QImage> #include <iostream> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QImage *img = new QImage("adadad.jpg"); //std::cout << "Type filename:" << std::endl; img->isNull(); return a.exec(); } 

This does not work. I got: (IT does not compile, but the file still does not exist ...)

 File not found: tmp/obj/debug_shared/main.o:: In function `main': 

What's happening? Is it impossible to use Qimage with a console application ?!

EDIT: screen

+4
source share
5 answers

You can use QImage in a console application, you must make sure that QtGui is configured. If you select a console application, your .pro file may contain something like

 CONFIG += console QT -= gui 

If this happens, delete the line QT - = gui.

+6
source

img-> isNull () does nothing on its own, try instead:

 if(img->isNull()) std::cout << "Image isNull!\n"; else std::cout << "Image loaded\n"; 

I assume that the local directory of the executable does not match the location of this image, so Qt cannot find the file. Try to specify the full path.

EDIT: Ah ... did not understand that this is a compilation problem. This looks suspiciously like a moc problem. What build system are you using? and can you confirm that the moc step is running?

0
source
 QImage("adadad.jpg"); 

You are probably looking for a file called adadad.jpg in the current working directory for your application. Check if this file is present. Otherwise, use the full path.

0
source

This modification of your code will compile and execute as expected if there is a valid image file in the current working directory when the application starts. It will display Image loaded

 #include <QtGui/QImage> #include <iostream> int main(int argc, char *argv[]) { QImage *img = new QImage("adadad.jpg"); if(img->isNull()) std::cout << "Image is null"; else std::cout << "Image loaded"; return 0; 

}

You do not need to instantiate QCoreApplication unless you subclass it and put your program code in this subclass.

Update: Your program does not exit, so you are probably getting this compilation error because it cannot replace the executable file because it is still running (and locked). More likely, file locking will be a problem in Windows.

-1
source

An important note when downloading a file using directly "adadad.jpg" in your code. Even if you put the file in the debug / release folder, QImage will always be NULL if it is loaded this way.

I ran into this problem yesterday and I fixed it using the Qt library to get the full path: QCoreApplication::applicationDirPath() .

There are two ways to achieve this, first, when you create an img object.

 QImage img( QCoreApplication::applicationDirPath() + "adadad.jpg"); if( img.isNull()) { qDebug() << "Loading Error - file: adadad.jpg."; return false; } 

or using the download function

 QImage img; if( !img.load(QCoreApplication::applicationDirPath() + "adadad.jpg")) { qDebug() << "Loading Error - file: adadad.jpg."; return false; } 
-1
source

All Articles