Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileDialog and concurrent access to QDir::currentDir()
Forum Updated to NodeBB v4.3 + New Features

QFileDialog and concurrent access to QDir::currentDir()

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 5.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabriele82rm
    wrote on last edited by
    #1

    Hi,

    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?

    Thanks
    Gabriele

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gabriele82rm
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          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);
          t->start();
          ...
          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.

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            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.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Franzk
              wrote on last edited by
              #6

              [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.

              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Darryl DSouza
                wrote on last edited by
                #7

                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.

                Darryl D'Souza

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved