Streight
Streight

Reputation: 861

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

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.

Upvotes: 4

Views: 8642

Answers (2)

Adrien BARRAL
Adrien BARRAL

Reputation: 3604

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.

Upvotes: 3

alexisdm
alexisdm

Reputation: 29886

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.

Upvotes: 4

Related Questions