Results 1 to 1 of 1

Thread: Weird behavior when filter QFileSystemModel by proxy

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Weird behavior when filter QFileSystemModel by proxy

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3.  
    4. class fileProxy : public QSortFilterProxyModel
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. explicit fileProxy(QObject *parent = 0) : QSortFilterProxyModel(parent), selectDirs_(true), selectFiles_(true) {}
    10.  
    11. public slots:
    12. void setSelectDirs(bool enable) { selectDirs_ = enable; }
    13. void setSelectFiles(bool enable) { selectFiles_ = enable; }
    14.  
    15. protected:
    16. bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
    17. {
    18. QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
    19. QFileSystemModel *source = static_cast<QFileSystemModel*>(sourceModel());
    20. QFileInfo info = source->fileInfo(index);
    21.  
    22. if(info.isDir() && selectDirs_) return true;
    23.  
    24. if(info.isFile() && selectFiles_) return true;
    25.  
    26. return false;
    27. }
    28.  
    29. private:
    30. bool selectDirs_;
    31. bool selectFiles_;
    32.  
    33. };
    34.  
    35. class Proxy2 : public QWidget
    36. {
    37. Q_OBJECT
    38.  
    39. public:
    40. Proxy2()
    41. {
    42. model_ = new QFileSystemModel(this);
    43.  
    44. normalProxy_ = new QSortFilterProxyModel(this);
    45. normalProxy_->setSourceModel(model_);
    46.  
    47. proxy_ = new fileProxy(this);
    48. proxy_->setSourceModel(model_);
    49.  
    50. treeView_ = new QTreeView(this);
    51. treeView_->setModel(normalProxy_);
    52. treeView_->setRootIndex(normalProxy_->mapFromSource(model_->setRootPath("")));
    53.  
    54. tableView_ = new QTableView(this);
    55. tableView_->setModel(proxy_);
    56.  
    57. connect(treeView_, SIGNAL(clicked(QModelIndex)), this, SLOT(changeTableRootIndex(QModelIndex)));
    58.  
    59. QCheckBox *dirBox = new QCheckBox(this);
    60. QCheckBox *fileBox = new QCheckBox(this);
    61.  
    62. dirBox->setText("Dir");
    63. fileBox->setText("file");
    64. dirBox->setCheckState(Qt::Checked);
    65. fileBox->setCheckState(Qt::Checked);
    66. connect(dirBox, SIGNAL(clicked(bool)), proxy_, SLOT(setSelectDirs(bool)));
    67. connect(fileBox, SIGNAL(clicked(bool)), proxy_, SLOT(setSelectFiles(bool)));
    68.  
    69. QHBoxLayout *layout = new QHBoxLayout(this);
    70. layout->addWidget(treeView_);
    71. layout->addWidget(tableView_);
    72. layout->addWidget(dirBox);
    73. layout->addWidget(fileBox);
    74.  
    75. setLayout(layout);
    76. }
    77.  
    78. public slots:
    79. void changeTableRootIndex(QModelIndex const &index)
    80. {
    81. QModelIndex sourceIndex = normalProxy_->mapToSource(index);
    82. tableView_->setRootIndex(proxy_->mapFromSource(sourceIndex));
    83. }
    84.  
    85. private:
    86. QFileSystemModel *model_;
    87. QSortFilterProxyModel *normalProxy_;
    88. fileProxy *proxy_;
    89. QTableView *tableView_;
    90. QTreeView *treeView_;
    91. };
    92.  
    93. inline int testProxy2(QApplication &app)
    94. {
    95. Proxy2 p2;
    96. p2.show();
    97.  
    98. return app.exec();
    99. }
    To copy to clipboard, switch view to plain text mode 

    I hope the fileProxy will filter the files according to the checkstate of the check box
    But the results are pretty weird

    1 : It would not filter out the files or directories instantly, I have to switch between different directories or drives
    of the treeView
    2 : Even I did switch the directories or drives, there is no guarantee the fileProxy will filter out the directories or
    files

    What kind of mistakes do I make?
    Thanks a lot


    Added after 4 minutes:


    I narrow the problem a little bit
    If I remove line 22, neglect all of the check box and change the codes to
    Qt Code:
    1. if(info.isFile() && selectFiles_) return true;
    To copy to clipboard, switch view to plain text mode 

    The TableView will never show anything
    How could I show the files(only files)to the users?
    Thanks a lot
    Last edited by stereoMatching; 27th July 2012 at 09:25.

Similar Threads

  1. How to filter duplicates from a proxy model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 6
    Last Post: 14th October 2016, 10:21
  2. Proxy for the QFileSystemModel
    By stereoMatching in forum Newbie
    Replies: 0
    Last Post: 26th July 2012, 12:26
  3. Weird removeRow behavior
    By metalinspired in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2009, 12:42
  4. Weird QMenu behavior on Mac
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 14th January 2009, 15:18
  5. Filter Proxy Model to Autoupdate View
    By Big Duck in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2006, 20:32

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.