0

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
}
4
  • Move the QPixmap::fromImage(mainImage) to a separate line so you can try to determine whether it's that code or the QPixmap constructor.
    – Sam
    Jul 2, 2015 at 1:46
  • Hm strange, perhaps the image is corrupt or invalid in some way. Are you using the debug versions of the Qt libraries? Do you have a callstack for when the crash occurs?
    – Sam
    Jul 2, 2015 at 4:20
  • Try to specify file format
    – t3ft3l--i
    Jul 2, 2015 at 8:35
  • @sam : The image is OK, if if corrupt on the first code the the mainImage should be null, but it is not. And I check the png file has no problem when opened with other application. Call stack stopped at the segmentation fault line. t3ft3l : Already test it, but it has the same problem.
    – Apin
    Jul 2, 2015 at 22:41

1 Answer 1

2

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. :)

1
  • Thanks for the update. I would have thought there would be an assert or error message about this requirement. Perhaps next time try running with the debug libraries. Also next time post the callstack; it can help with debugging!
    – Sam
    Jul 6, 2015 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.