4

With the method/command:

OpenCreateDirectory() 
{  
    QString Directory = QFileDialog::getExistingDirectory(this,
                        tr("Choose Or Create Directory"),
                        "/home",
                        QFileDialog::DontResolveSymlinks);
}

I can create a new directory or choose an existing one. Is there a way to disable the possibility to create a new directory? Also, is there a way to disable the possibility to choose an existing directory?

To be more precise: When I use the method above, a window pops up in which I can create a new directory OR open an existing directory. What I want to do is to limit the method, so that I just can create a new directory without being able to just open an existing directory OR in the other case limit the method, so that I just can open an existing directory without being able to create a new directory.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Streight
  • 811
  • 4
  • 15
  • 28

2 Answers2

4

You can prevent the creation of a new directory by using these options:

QFileDialog::DontUseNativeDialog | QFileDialog::ReadOnly 
| QFileDialog::ShowDirsOnly

The options ReadOnly doesn't have any effect if you use native dialogs, at least on Windows.


And for disabling the choice of an already existing directory, you can't.
But you could either add the name of the directory to be created as a separate option, or check that the chosen directory is not empty.
alexisdm
  • 29,448
  • 6
  • 64
  • 99
  • This works fine. I just left the "| QFileDialog::ReadOnly | QFileDialog::ShowDirsOnly" out of it, because these arguments are not necessary. I accept this answer. greetings – Streight Mar 08 '12 at 15:02
  • 1
    Well if going non-native you might wish to create a manual file dialog screen? and use `setFileMode()` – paul23 Mar 08 '12 at 18:11
  • @Streight According to QFileDialog source code, ReadOnly is necessary to disable the "newFolderButton" and on Windows the button is enabled without that option. – alexisdm Mar 08 '12 at 19:24
  • Unfortunately "QFileDialog::DontUseNativeDialog" is required. Otherwise the button "New Directory" will still be visible. However, dont using the native dialog looks ugly on my OS. – Anonymous Jul 06 '17 at 09:14
3

Yes you can add the options QFileDialog::ReadOnly when you create your QFileDialog. So create it with :

QString Directory = QFileDialog::getExistingDirectory(this, tr("Choose Or Create Directory"),
                                                         "/home",
                                                           QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly);

The "Create Directory" button of the file dialog still exists, but you can't create the directory. I successfully used this feature on Ubuntu.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
Adrien BARRAL
  • 3,474
  • 1
  • 25
  • 37
  • Unfortunatly does not work. I added "| QFileDialog::ReadOnly", but I still can create a new directory with the method, I also run it on Ubuntu, the create button still exists and I can use it :D. – Streight Mar 08 '12 at 14:33
  • That is very strange... When I click on the button, i am able to set a name to a new folder, but when I click on enter (to finalize the folder creation) i have an error message wich said : "Error creating directory: Permission denied"... – Adrien BARRAL Mar 08 '12 at 16:12