QFileDialog: choose directories only but show files as well



  • Hello,
    I would like to have aQFileDialog from which i will be able to choose dirs but meanwhile i should be able to see files in them as well. I tired:
    QString path = QFileDialog::getExistingDirectory(this, tr("Choose catalog"), ".", QFileDialog::ReadOnly);
    I can choose dirs and thats ok but i cant see any files, any help?


  • Lifetime Qt Champion

    hi
    And
    QFileDialog::getOpenFileName();
    is not what you want?
    It's a normal file open?



  • I want to pick dirs ONLY (so the result of if would be the path to the DIR), but able to see files as well. Like you want to choose DIR to read all files of some type from but you want to be able to see the files as well so you know you pick correct folder. So getOpenFileName() doesnt suites here exatcly (or i customize it wrong...).


  • Lifetime Qt Champion

    @michelson
    Ok. I understand.
    Not sure how you can make it show the files but only
    allow to press ok on folder. Not in any easy way.

    It is not an option to let the user also select a file and just snag the path from it ?



  • You know, i CAN do that (i mean its just home-use app so its not a big deal it wont work as i want) but for the sake of good-looks of a whole project i wanted to at least try to implement this one thing :D


  • Lifetime Qt Champion

    @michelson
    Well there is a lot of flags. But there is not one that sounds like such function.
    (in my head)
    You could make your own dialog but that
    somewhat a lot of work to NOT select a file.

    You could also make it 2 step.
    First select folder
    Then dialog comes with all files , each checked and u can exclude some if needed
    or else press ok to proceed.
    That way is 100% want you want and serves a function.



  • QFileDialog dialog;
    dialog.setFileMode(QFileDialog::DirectoryOnly);
    dialog.setOption(QFileDialog::ShowDirsOnly, false);
    dialog.exec();
    qDebug() << dialog.directory();
    

  • Lifetime Qt Champion

    @Hamed.Masafi
    Hi
    it looks good but its not showing files here on win 7.
    Only the dirs.
    On your platform, it does show all files also?



  • @mrjj said:

    Hi
    it looks good but its not showing files here on win 7.
    Only the dirs.
    On your platform, it does show all files also?

    Hi, on my platform (debian + kde) works well. But we can take a test.
    Please change second line to flowing code and test again.

    dialog.setFileMode(QFileDialog::Directory);
    

  • Lifetime Qt Champion

    @Hamed.Masafi
    Ok, must be different on win
    Hmm am i blind or is that same line?



  • Oh, sorry. My mistake

    dialog.setFileMode(QFileDialog::Directory);
    


  • @Hamed.Masafi
    Unfortunetly i can confirm @mrjj issiue - Win7 OS still couses files to be invisible. I guess problem cant be solved easily @ Win OS - i think the only way is to reimplement QFileDialog which for me is just not worth the time it will take for such trivial thing.



  • I've tested on windows. It seems the only possible solution is to ignore native dialog.

    QFileDialog dialog;
    dialog.setFileMode(QFileDialog::DirectoryOnly);
    dialog.setOption(QFileDialog::DontUseNativeDialog, true);
    dialog.setOption(QFileDialog::ShowDirsOnly, false);
    dialog.exec();
    qDebug() << dialog.directory();
    

Log in to reply