I have reworked a Qt program that was originally in Windows such that it would work properly in both Windows and Linux.
Part of my code is designed to write a file in the program's directory:
QFile file(filename
);
//open the file: if (file.
open(QFile::WriteOnly)) { qDebug() << "File open: " << filename;
file.write(filedata);
file.close();
} else {
qDebug() << "File not open: " << filename;
}
QDir::setCurrent(QCoreApplication::applicationDirPath());
QFile file(filename); //open the file:
if (file.open(QFile::WriteOnly)) {
qDebug() << "File open: " << filename;
file.write(filedata);
file.close();
} else {
qDebug() << "File not open: " << filename;
}
To copy to clipboard, switch view to plain text mode
On windows, this code would create the file in the directory of the program
On linux 2 results happen:
--- If I click on the application in Konqueror or Dolphin, then the file is created in my home directory rather than where the executable is, seemingly ignoring the QDir
--- if I open a terminal and type "./myprogram" then the program creates the file in the directory of the program like it should.
Why would this happen, and how could I make the file always be created in the directory of the executable regardless of if its opened through the file manager or the command line?
I am using Qt 4.4.3 on an Ubuntu Intrepid and Windows XP systems.
Bookmarks