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

QFileDialog

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 5.4k Views
  • 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.
  • F Offline
    F Offline
    Felix
    wrote on last edited by
    #1

    Hi,

    I need the user to select either one folder or one file with a specific extension. Since the FileMode of QFileDialog is not a QFLag, i cant set FileMode (ExistingFile | Directory).

    Is there any trick or workaround to solve my problem?

    Thanks

    Felix

    1 Reply Last reply
    0
    • E Offline
      E Offline
      erapid
      wrote on last edited by
      #2

      http://www.qtcentre.org/threads/4810-QFileDialog-how-to-choice-File-or-Directory-in-the-same-Dialog

      In short, the only solution - write your own dialog. In discussing the details are correct: "It would be hard to create a combined directory and file dialog. Much because it would be impossible to tell if you pick a directory or just want so enter it to look for other directories or files."

      QFileDialog has only one button that which takes one of two states: Open (dir) | Select. But you need two buttons in the same time.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lyuts
        wrote on last edited by
        #3

        I think that you don't need to do that. Just
        @setFileMode(QFileDialog::Directory)@.
        Looks like this should be enough because the description for this flag says
        bq. The name of a directory. Both files and directories are displayed. eq.

        I'm a rebel in the S.D.G.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          erapid
          wrote on last edited by
          #4

          That's right, displays directories and files. But you can select only directories :) That said, the problem of an only one button

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lyuts
            wrote on last edited by
            #5

            Oh, I see. Well, the first that comes to mind is to create a widget which contains a button (or radio button group) and a file dialog. A click on a button changes the fileMode() from Directory to ExistingFile and vice versa. I'm not sure this will look great in terms of UI, but I don't see any other easy solution.

            I'm a rebel in the S.D.G.

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

              i think i a desktop application, having radiobuttons like that to choose a dialog is a bad solution. Also in my case the user neednt to know if it is a folder or a file, since it doesnt matter at all.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lyuts
                wrote on last edited by
                #7

                It was just an idea, I'm not forcing you to follow it :).

                I'm a rebel in the S.D.G.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  musto
                  wrote on last edited by
                  #8

                  I just run into a similar problem where the user needs to choose a directory or a file from a File Dialog
                  Here's my hack to solve it.

                  subclass your class from QFileDialog and set the FileMode to AnyFile
                  @

                  QFileSystemModel *model = new QFileSystemModel;
                  model->setRootPath(QDir::currentPath());
                  QStringList filenames;
                  QList<QUrl> urls;
                  urls << QUrl::fromLocalFile&#40;QDesktopServices::storageLocation(QDesktopServices::DesktopLocation&#41;&#41;
                       << QUrl::fromLocalFile&#40;QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation&#41;&#41;;
                  mfiledialog->setSidebarUrls(urls);
                  mfiledialog->setFileMode(QFileDialog::AnyFile);
                  mfiledialog->setViewMode(QFileDialog::Detail);
                  

                  Then have a slot to change FileMode on currentchanged signal as follows

                  void MainWindow::on_mfiledialog_currentchanged(QString filedir)
                  {
                  QFileInfo finfo = QFileInfo(filedir);
                  if(finfo.isDir())
                  mfiledialog->setFileMode(QFileDialog::Directory);
                  else
                  mfiledialog->setFileMode(QFileDialog::AnyFile);
                  }
                  @

                  Edit: please put @ tags around code sections; Andre

                  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