How to specify the QFileDialog::getExistingDirectory() method?

Streight picture Streight · Mar 8, 2012 · Viewed 7.3k times · Source

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.

Answer

alexisdm picture alexisdm · Mar 8, 2012

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.