4

I am trying to do console application to read pixels from 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();
}

That doesn't work I got: (IT doesn't compile, but anyway file isn't exist yet...)

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

What is going on? Is it impossible to use Qimage with console app?!

EDIT: screen

John X
  • 499
  • 2
  • 7
  • 15

5 Answers5

8

It is possible to use QImage in a console application, you must make sure that QtGui is configured though. If you chose a console app, your .pro file might contain something like

CONFIG += console
QT -= gui

If that's the case, remove the QT -= gui line.

Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • 1
    Don't forget to delete the build directories. I remembered to add the GUI module, but forgot to do that, and it kept complaining about missing include files even though they showed up in code completion. Running "Clean all" from Qt Creator is not enough. – Daniel Saner Apr 08 '15 at 15:12
  • @Daniel Saner: I never experienced that when it comes to module handling. A rerun of qmake (which should even happen automatically when the .pro file was touched) should suffice. – Frank Osterfeld Apr 08 '15 at 21:01
  • 1
    @Frank Osterfeld: Strange, you must be very lucky or I must be doing something wrong. Changes to .pro or moc_ files almost always require a manual build directory delete for me, not even a _Clean All_ will suffice. I've heard from others, too, that it tends to fix issues with projects that should build, but don't. – Daniel Saner Apr 10 '15 at 13:28
  • @Daniel Saner: In Qt Creator? If yes, which creator version and which Qt version would that be? The only issues I'm aware of is about headers included via INCLUDEPATH, and when using static convenience libs in the project. See here: http://stackoverflow.com/questions/5470438/why-are-changes-in-source-not-always-reflected-in-machine-code-after-build-while/5470829#5470829 – Frank Osterfeld Apr 10 '15 at 18:19
  • @DanielSaner I've found that source control is the cause of Qt incorrectly building in my case. It assumes only files changed in QtCreator need rebuilding so if git updates a file I have to manually delete the build directory to get it building correctly. – Troyseph Jan 11 '16 at 10:46
0

img->isNull() doesn't do anything on it's own, try this instead:

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

My guess is that the local directory of the executable is not the same as the location of that image, so Qt can't find the file. Try specifying the complete path.

EDIT: Ahh... didn't realize it was a compilation problem. That looks suspiciously like a moc issue. What build system are you using? and can you confirm that the moc step is executing?

jkerian
  • 16,497
  • 3
  • 46
  • 59
0
QImage("adadad.jpg");

Will probably look for a file called adadad.jpg on the current working directory for your application. Check if that file is present. Otherwise, use a fully qualified path.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
-1

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

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

There is two way to achieve that, first one is when you create the img object.

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

or using the load function

QImage img;
if( !img.load(QCoreApplication::applicationDirPath() + "adadad.jpg"))
{
    qDebug() << "Loading Error - file: adadad.jpg.";
    return false;
} 
ForceMagic
  • 6,230
  • 12
  • 66
  • 88
-1

This modification of your code will compile and run as expected if there is a valid image file in the current working directory when you run the app. 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 create an instance of QCoreApplication unless you have subclassed it and put your program code in that subclass.

Update: Your program does not exit so you are probably getting that compile error because it can't replace the executable because it is still running (and locked). The file locking is more likely to be an issue under Windows.

Arnold Spence
  • 21,942
  • 7
  • 74
  • 67