Newbie problem with QT C++ - Qimage dont work?

蹲街弑〆低调 提交于 2019-12-12 11:30:11

问题


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


回答1:


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.




回答2:


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.




回答3:


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?




回答4:


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.




回答5:


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;
} 


来源:https://stackoverflow.com/questions/3596258/newbie-problem-with-qt-c-qimage-dont-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!