-1

I have kept the Content.xml in the same folder where my app.exe exists.

But open call returns false ?

What should be the exact path for the file ?

I do not want to give the complete path for the file but want my code to be path independent "means file which i want to read should be in same folder where my exe is lying"

#define FILE_NAME "Content.xml"

QString xmlFileName(FILE_NAME);
xmlFile.setFileName(xmlFileName);

if ( ! xmlFile.open(QIODevice::ReadOnly|QIODevice::Text) )
{
    QMessageBox* msgBox = new QMessageBox();
    msgBox->setText("File Not Found !!");
    msgBox->setWindowFlags(Qt::WindowStaysOnTopHint);
    msgBox->exec();
}
p.i.g.
  • 2,815
  • 2
  • 24
  • 41
Katoch
  • 2,709
  • 9
  • 51
  • 84
  • 1
    What platform are you running? Windows will be different from OSX. In addition, why not add Content.xml to a Qt resource file? http://qt-project.org/doc/qt-5/resources.html – TheDarkKnight Jul 22 '14 at 07:49
  • possible duplicate of [Find the installation directory of a Qt application](http://stackoverflow.com/questions/19057482/find-the-installation-directory-of-a-qt-application) – László Papp Jul 22 '14 at 08:07
  • 1
    @Katoch: just add the application dir path from the other answer to your setFileName call before the xmlFileName, and it will work. – László Papp Jul 22 '14 at 08:12
  • `xmlFile.setFileName(QCoreApplication::applicationDirPath() + "/" + xmlFileName)` – László Papp Jul 22 '14 at 08:25
  • thanks @FinalContest this link also answer the question http://stackoverflow.com/questions/23381556/qfile-not-opening-finding-file. – Katoch Jul 22 '14 at 09:01

2 Answers2

1

Try to open file with full path like:

xmlFile.setFileName(QCoreApplication::applicationDirPath() + QLatin1Char('/') + xmlFileName);

nib
  • 700
  • 8
  • 15
-1

If you run your code with the run button from Qt Creator, the Content.xml needs to be in the same folder as your code is.

user3382781
  • 11
  • 1
  • 3
  • This is obviously wrong an advice since the code should be independent of the running folder, and not moving the file always around when you want to run it from different directories. This question has been asked many times on Stack Overflow, so it is a duplicate after all. – László Papp Jul 22 '14 at 08:08