Apin
Apin

Reputation: 2668

QPixmap load segmentation fault

I am creating a Qt Console Application on windows 7. I am using Qt 5.3. And currently I have error that make me frustration because of it. Already check on the stackoverflow, but no answers help me.

My problem was when creating a Qpixmap I got error segmentation fault and I don't have any other error information about it.

Here my code :

QString filePath = (directory + xmlReader.attributes().value("relativepath").toString());
QFile _file(filePath);
if (!_file.exists())
{
    qWarning() << "Error : file " << filePath << " does not exist";
    return false;
}

QImageReader imageReader(filePath);
QImage mainImage = imageReader.read();
if(mainImage.isNull())
{
    qWarning() << "Error read image : " << filePath;
    qWarning() << imageReader.errorString();
    return false;
}
QPixmap mainPixmap(QPixmap::fromImage(mainImage)); // Segmentation fault here

On the last line of those code that generate the segmentation fault error on my machine. Is there anything i can do to debug this error?

Update : I have some code on QPixmap, but it lead the same segmentation fault.

Code 1 :

QString filePath = (directory + xmlReader.attributes().value("relativepath").toString());
QFile _file(filePath);
if (!_file.exists())
{
    qWarning() << "Error : file " << filePath << " does not exist";
    return false;
}

QImageReader imageReader(filePath);
QImage mainImage = imageReader.read();
if(mainImage.isNull())
{
    qWarning() << "Error read image : " << filePath;
    qWarning() << imageReader.errorString();
    return false;
}    
QPixmap mainPixmap;
mainPixmap = QPixmap::fromImage(mainImage); // segmentation fault here

Code 2 :

QString filePath = (directory + xmlReader.attributes().value("relativepath").toString());
QFile _file(filePath);
if (!_file.exists())
{
    qWarning() << "Error : file " << filePath << " does not exist";
    return false;
}
QPixmap mainPixmap;
mainPixmap.load(filePath); // segmentation fault here

Code 3 :

QString filePath = (directory + xmlReader.attributes().value("relativepath").toString());
QFile _file(filePath);
if (!_file.exists())
{
    qWarning() << "Error : file " << filePath << " does not exist";
    return false;
}
QPixmap mainPixmap;
if(_file.open(QFile::ReadOnly))
{
    mainPixmap.loadFromData(_file.readAll()); //segmentation fault here
}

Upvotes: 0

Views: 1339

Answers (1)

Apin
Apin

Reputation: 2668

Finally I have figured out what is the problem. The problem is I need to have QGuiApplication instance before using the QPixmap.

On console application I usually has QCoreApplication as the root QApplication, but when we want to use QPixmap we need to change it to QGuiApplication on main.cpp.

Hope this help others. :)

Upvotes: 2

Related Questions