PDA

View Full Version : Problem with filter in Qdialog



Spinter
2nd December 2014, 10:15
Well, my problem is I only want to see the directories that are writeable and save the path of the selected one in a QString


I have the next code but I dont know how to apply exactly the filter.




QString directoryName = QFileDialog::getExistingDirectory(
this,tr("Select directory"),
"/home/"
"QDir::Writable");

anda_skoa
2nd December 2014, 10:52
The static convenience function can't do that.
You need an explicit instance.

Cheers,
_

Spinter
2nd December 2014, 11:14
I thougth about that then I have that code too



QString directoryName = "";
QFileDialog explorador;
explorador.getExistingDirectory(this);
explorador.setFilter(QDir::Writable);
explorador.tr("Select a directory");
explorador.directory("/home");


Also I think code is wrong xD
And then the problem is how do I save the path of the selected directory????

Lesiok
2nd December 2014, 11:31
I thougth about that then I have that code too



QString directoryName = "";
QFileDialog explorador;
explorador.getExistingDirectory(this);
explorador.setFilter(QDir::Writable);
explorador.tr("Select a directory");
explorador.directory("/home");


Also I think code is wrong xD
And then the problem is how do I save the path of the selected directory????

But this code does not make sense. In line 3 You are calling static method without saving the result.
First You should set all parameters and then do something like this :
QStringList fileNames;
if (dialog.exec())
fileNames = dialog.selectedFiles();
All of this is shown here (http://qt-project.org/doc/qt-4.8/qfiledialog.html#details)

Spinter
2nd December 2014, 11:56
Well I do it in that way but I am still having the same error



QString directoryName = "";
QFileDialog *explorador = new QFileDialog(this);
explorador->setFileMode(QFileDialog::DirectoryOnly);
explorador->setFilter(QDir::Writable);
explorador->setWindowTitle(QFileDialog::tr("Select a folder"));
explorador->setDirectory(QFileDialog::tr("/home"));
explorador->exec();
directoryName = explorador->selectedFiles().front();



The error is that I am still able to select folders where normal users (not superuser) aren't allow to write.

Lesiok
2nd December 2014, 12:02
Read once more doc for QDir::Filters (http://qt-project.org/doc/qt-4.8/qdir.html#Filter-enum).

Spinter
2nd December 2014, 12:15
Yes I read it but then it is no possible to select directories that cant be writeable by normal users with filters???, I mean if writeable list files, how I list directories???.

I dont get it.

you need to use QDir::Dirs after the first filter???

Lesiok
2nd December 2014, 12:42
I think You don't read... From the doc of value QDir::Writable List files for which the application has write access. The Writable value needs to be combined with Dirs or Files.
So You should use it like this
explorador->setFilter(QDir::Writable | QDir::QDir::Dirs);