QFileDialog



  • Hi,

    I need the user to select either one folder or one file with a specific extension. Since the FileMode of QFileDialog is not a QFLag, i cant set FileMode (ExistingFile | Directory).

    Is there any trick or workaround to solve my problem?

    Thanks

    Felix



  • http://www.qtcentre.org/threads/4810-QFileDialog-how-to-choice-File-or-Directory-in-the-same-Dialog

    In short, the only solution - write your own dialog. In discussing the details are correct: "It would be hard to create a combined directory and file dialog. Much because it would be impossible to tell if you pick a directory or just want so enter it to look for other directories or files."

    QFileDialog has only one button that which takes one of two states: Open (dir) | Select. But you need two buttons in the same time.



  • I think that you don't need to do that. Just
    @setFileMode(QFileDialog::Directory)@.
    Looks like this should be enough because the description for this flag says
    bq. The name of a directory. Both files and directories are displayed. eq.



  • That's right, displays directories and files. But you can select only directories :) That said, the problem of an only one button



  • Oh, I see. Well, the first that comes to mind is to create a widget which contains a button (or radio button group) and a file dialog. A click on a button changes the fileMode() from Directory to ExistingFile and vice versa. I'm not sure this will look great in terms of UI, but I don't see any other easy solution.



  • i think i a desktop application, having radiobuttons like that to choose a dialog is a bad solution. Also in my case the user neednt to know if it is a folder or a file, since it doesnt matter at all.



  • It was just an idea, I'm not forcing you to follow it :).



  • I just run into a similar problem where the user needs to choose a directory or a file from a File Dialog
    Here's my hack to solve it.

    subclass your class from QFileDialog and set the FileMode to AnyFile
    @

    QFileSystemModel *model = new QFileSystemModel;
    model->setRootPath(QDir::currentPath());
    QStringList filenames;
    QList<QUrl> urls;
    urls << QUrl::fromLocalFile&#40;QDesktopServices::storageLocation(QDesktopServices::DesktopLocation&#41;&#41;
         << QUrl::fromLocalFile&#40;QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation&#41;&#41;;
    mfiledialog->setSidebarUrls(urls);
    mfiledialog->setFileMode(QFileDialog::AnyFile);
    mfiledialog->setViewMode(QFileDialog::Detail);
    

    Then have a slot to change FileMode on currentchanged signal as follows

    void MainWindow::on_mfiledialog_currentchanged(QString filedir)
    {
    QFileInfo finfo = QFileInfo(filedir);
    if(finfo.isDir())
    mfiledialog->setFileMode(QFileDialog::Directory);
    else
    mfiledialog->setFileMode(QFileDialog::AnyFile);
    }
    @

    Edit: please put @ tags around code sections; Andre


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.