+ Reply to Thread
Results 1 to 4 of 4

Thread: QFileDialog to select files AND folders

  1. #1
    Join Date
    Jul 2011
    Posts
    42
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QFileDialog to select files AND folders

    The problem that i have i that QFileDialog doesnt seem to support choosing files or folders. FileMode flags dont have one that mixes both.


    Is there any way to make a QFileDialog able to select files and folders?

    FileMode::AnyFiles let you select a file, but if you choose a folder it opens it and shows you the files in it and FileMode:irectory only let you select folders. Since im doing a compressor i must allow the user to select files, folders, or both at the same time (multiple files and folders). Is there any other way that doesnt require craeting a new dialog from scratch?

    thanks!

  2. #2
    Join Date
    May 2012
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QFileDialog to select files AND folders

    I tried to handle the same problem and I found a solution after searching in QT forums. The following is the codes that you could try:
    Qt Code:
    1. QFileDialog* _f_dlg = new QFileDialog(this);
    2. _f_dlg->setFileMode(QFileDialog::Directory);
    3. _f_dlg->setOption(QFileDialog::DontUseNativeDialog, true);
    4.  
    5. // Try to select multiple files and directories at the same time in QFileDialog
    6. QListView *l = _f_dlg->findChild<QListView*>("listView");
    7. if (l) {
    8. l->setSelectionMode(QAbstractItemView::MultiSelection);
    9. }
    10. QTreeView *t = _f_dlg->findChild<QTreeView*>();
    11. if (t) {
    12. t->setSelectionMode(QAbstractItemView::MultiSelection);
    13. }
    14.  
    15. int nMode = _f_dlg->exec();
    16. QStringList _fnames = _f_dlg->selectedFiles();
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ijab for this useful post:

    superpacko (15th May 2012)

  4. #3
    Join Date
    Jul 2011
    Posts
    42
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFileDialog to select files AND folders

    Thanks! that's a nice workaround! i'll try it

  5. #4
    Join Date
    Apr 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFileDialog to select files AND folders

    This is the way i'm make it possible, by subclass QFileDialog, connect "Choose" button signal to yourself slot, and always setEnable(true) for "Choose" button
    Qt Code:
    1. //Subclass QFileDialog for customize allow select both file/folder
    2. class FileDialog : public QFileDialog{
    3. Q_OBJECT
    4. private:
    5. QListView *m_listView;
    6. QTreeView *m_treeView;
    7. QPushButton *m_btnOpen;
    8. QStringList m_selectedFiles;
    9.  
    10. public slots:
    11. void chooseClicked();
    12. public:
    13. FileDialog();
    14. QStringList selectedFiles();
    15. bool eventFilter(QObject* watched, QEvent* event);
    16. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. FileDialog::FileDialog() : QFileDialog()
    2. {
    3. m_btnOpen = NULL;
    4. m_listView = NULL;
    5. m_treeView = NULL;
    6. m_selectedFiles.clear();
    7.  
    8. this->setOption(QFileDialog::DontUseNativeDialog, true);
    9. this->setFileMode(QFileDialog::Directory);
    10. QList<QPushButton*> btns = this->findChildren<QPushButton*>();
    11. for (int i = 0; i < btns.size(); ++i) {
    12. QString text = btns[i]->text();
    13. if (text.toLower().contains("open") || text.toLower().contains("choose"))
    14. {
    15. m_btnOpen = btns[i];
    16. break;
    17. }
    18. }
    19.  
    20. if (!m_btnOpen) return;
    21.  
    22. m_btnOpen->installEventFilter(this);
    23. //connect(m_btnOpen, SIGNAL(changed()), this, SLOT(btnChanged()))
    24. m_btnOpen->disconnect(SIGNAL(clicked()));
    25. connect(m_btnOpen, SIGNAL(clicked()), this, SLOT(chooseClicked()));
    26.  
    27.  
    28. m_listView = findChild<QListView*>("listView");
    29. if (m_listView) {
    30. m_listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    31. }
    32.  
    33. m_treeView = findChild<QTreeView*>();
    34. if (m_treeView) {
    35. m_treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    36. }
    37.  
    38. }
    39.  
    40. bool FileDialog::eventFilter( QObject* watched, QEvent* event )
    41. {
    42. QPushButton *btn = qobject_cast<QPushButton*>(watched);
    43. if (btn)
    44. {
    45. if(event->type()==QEvent::EnabledChange) {
    46. if (!btn->isEnabled()) {
    47. btn->setEnabled(true);
    48. }
    49. }
    50. }
    51.  
    52. return QWidget::eventFilter(watched, event);
    53. }
    54.  
    55.  
    56. void FileDialog::chooseClicked()
    57. {
    58. QModelIndexList indexList = m_listView->selectionModel()->selectedIndexes();
    59. foreach (QModelIndex index, indexList)
    60. {
    61. if (index.column()== 0)
    62. {
    63. m_selectedFiles.append(this->directory().absolutePath() + index.data().toString());
    64. }
    65. }
    66.  
    67. QDialog::accept();
    68. }
    69.  
    70. QStringList FileDialog::selectedFiles()
    71. {
    72. return m_selectedFiles;
    73. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by tranhuanltv; 16th April 2013 at 13:12.

+ Reply to Thread

Similar Threads

  1. Replies: 8
    Last Post: 15th May 2012, 05:21
  2. Replies: 0
    Last Post: 11th February 2011, 04:48
  3. Qt select folders and files dialog.
    By bunjee in forum Qt Programming
    Replies: 5
    Last Post: 13th July 2009, 08:53
  4. Select multiple files from QFileDialog
    By jiveaxe in forum Qt Programming
    Replies: 6
    Last Post: 16th February 2009, 14:57
  5. Replies: 1
    Last Post: 5th October 2007, 09:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts