Results 1 to 4 of 4

Thread: QFileDialog to select files AND folders

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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.

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
  •  
Qt is a trademark of The Qt Company.