QFileDialog and concurrent access to QDir::currentDir()
I have an app composed of two threads.
The main thread opens a QFileDialog through the instruction "QFileDialog::getOpenFileName(this, tr("Import Map File"))".
The other thread saves some files according to a path composed from QDir::currentDir(), BUT is no related with the previous QFileDialog at all. Neither the two threads are related,they are completely independent.
I experience the following problem. As soon as the user browses the file system in the opened QFileDialog, it seems that she is also changing the current directory, which is used by the secondary thread to save the files. The effect is that, instead of saving the files all in the same directory, the secondary thread spread the files in all the directories that the main thread is visiting through the qfiledialog.
Should I stop the secondary thread till the qfiledialog is closed, or is it possible to deal with this concurrency somehow? IMHO I think that QFileDialog should not change the current dir of all the app. Any idea?
Errr... How about you simply cache the value of QDir::currentDir() in your second thread, and used that value instead of asking QDir::currentDir() all the time?
Well of course :)
Simply it seems to me strange that QFileDialog actually alters the current dir of the whole app. I would rather imagine QFileDialog managing this stuff independently.
Thanks for your hint.
I'm not sure if QFileDialog actually does change the current directory, and if it does, then maybe it shouldn't. But isn't it better design not to depend on the current directory -- i.e. pass the current directory to the thread upon creation? Do you really always want to write to the current directory? What if you start a second thread that is going to write to a different directory? Or put in different words: the current directory is a global state not a thread local state. Don't depend on mutable global states. Make a snapshot of what you need, and work with that.
@WriterThread *t = new WriterThread(QDir::currentDirectory(), this);
QFileDialog::getOpenFileName(this, tr("Import map file"));@
You're working with threads here, and you don't want thread 1 to be affected by changes in thread 2 without explicit communication.
And beaten to it. Anyway, consider the above.
Why do you use currentDir? do you want the starting dir of the app or the directory of the binary? A process can change it's current directory. so I would suggest to go for startup dir or path of the exe.
[quote author="Gerolf" date="1297859508"]Why do you use currentDir? do you want the starting dir of the app or the directory of the binary? A process can change it's current directory. so I would suggest to go for startup dir or path of the exe.[/quote]Writing to the applicationDirPath() isn't a good idea, as it will cause confusion with Windows' UAC and the actual location the data is written to, and on linux you might not even be sure what the applicationDirPath() is outside of the program. Best is to use QDesktopServices::storageLocation() for these kinds of things. It's portable and predictable to both developer and user.
The simplest way would be using the QDir::currentDirectory() in second thread and there after not using the QDir::currentDirectory() method at all.
You can also use the QDir::temp() to temporarily save the files.