PDA

View Full Version : Strange File Path Issue



tpf80
25th November 2008, 23:47
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:



QDir::setCurrent(QCoreApplication::applicationDirP ath());

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;
}



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.

tpf80
26th November 2008, 05:59
nvm, I guess this is a known bug in KDE, which the file manager forces the default working directory to be home unless you make a link to the file and change the working directory that way or run from the command line.

tpf80
26th November 2008, 06:29
QFile file(qApp->applicationDirPath() + "/" + filename); //open the file:
if (file.open(QFile::WriteOnly)) {
qDebug() << "File open: " << filename;
file.write(filedata);
file.close();

} else {
qDebug() << "File not open: " << filename;
}

did the trick

pgorszkowski
26th November 2008, 06:48
Yes , you are right. This is known bug in KDE:

http://bugs.kde.org/show_bug.cgi?id=131010

There is a solution(it's not user friendly but...)