Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top