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:

Qt Code:
  1. QDir::setCurrent(QCoreApplication::applicationDirPath());
  2.  
  3. QFile file(filename); //open the file:
  4. if (file.open(QFile::WriteOnly)) {
  5. qDebug() << "File open: " << filename;
  6. file.write(filedata);
  7. file.close();
  8. } else {
  9. qDebug() << "File not open: " << filename;
  10. }
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.