Results 1 to 3 of 3

Thread: Using QFileSystemModel, QItemSelectionModel, QSortFilterProxyModel

  1. #1
    Join Date
    Jul 2013
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Using QFileSystemModel, QItemSelectionModel, QSortFilterProxyModel

    Hi All,

    My first Qt GUI App ... been coding C for many many years, have simple basic C++ skills, no GUI experience, Very little OO experience.
    So ALL explained comments and suggestions welcome.

    WinXPsp3
    Qt5.0.2 and Qt Creator 2.7.0
    TagLib2 (should work with all versions)

    I started with the Qt GUI Application, and started adding bit by bit in the following steps.
    1. directoryTreeView (QTreeView) and QFSMdirectoryView (QFileSystemModel), got it working.
    2. filenameListView (QListView) and QFSMfilenameView (QFileSystemModel), got it working.
    3. frameListView (QListView) and QSIMframeView (QStandardItemModel), got it working.

    All works as expected ... Mouse only though, So I added:
    4. directorySelectionModel (QItemSelectionModel), hooked it all up and got it working with the keyboard then added:
    5. filenameSelectionModel (QItemSelectionModel), hooked it all up and got it working with the keyboard.

    Now all is good everything works as expected, Except sorting and filtering, So I added:
    6. directoryProxyModel (QSortFilterProxyModel) for SORT purposes.

    Sorting the directoryTreeView works ... However:

    Question:
    How do I get the directoryTreeView selected path (used to setRootIndex for filenameListView) in
    Qt Code:
    1. void MainWindow::directorySelectionChanged(...)
    To copy to clipboard, switch view to plain text mode 
    Looking at (at very end of MainWindow.cpp)
    Qt Code:
    1. void MainWindow::directorySelectionChanged(...)
    To copy to clipboard, switch view to plain text mode 
    one can see how I did it prior to adding QSortFilterProxyModel.

    MainWindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QFileSystemModel>
    6. #include <QStandardItemModel>
    7. #include <QItemSelectionModel>
    8. #include <QSortFilterProxyModel>
    9. #include <QMessageBox>
    10.  
    11. #include <mpegfile.h>
    12. #include <id3v2tag.h>
    13.  
    14. #define MyQStringToTString(s) TagLib::String(s.toUtf8().data(), TagLib::String::UTF8)
    15.  
    16. namespace Ui {
    17. class MainWindow;
    18. }
    19.  
    20. class MainWindow : public QMainWindow
    21. {
    22. Q_OBJECT
    23.  
    24. public:
    25. explicit MainWindow(QWidget *parent = 0);
    26. ~MainWindow();
    27.  
    28. private slots:
    29. void on_actionExit_triggered();
    30.  
    31. void directorySelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
    32.  
    33. void filenameSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
    34.  
    35. private:
    36. Ui::MainWindow *ui;
    37. QFileSystemModel *QFSMdirectoryView;
    38. QFileSystemModel *QFSMfilenameView;
    39. QStandardItemModel *QSIMframeView;
    40. QItemSelectionModel *directorySelectionModel;
    41. QItemSelectionModel *filenameSelectionModel;
    42. QSortFilterProxyModel *directoryProxyModel;
    43. };
    44.  
    45. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    MainWindow.cpp
    Qt Code:
    1. #include "MainWindow.h"
    2. #include "ui_MainWindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    5. {
    6. ui->setupUi(this);
    7.  
    8. QString qsPath = QDir::currentPath();
    9.  
    10. QFSMdirectoryView = new QFileSystemModel(this);
    11. QFSMdirectoryView->setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
    12.  
    13. directoryProxyModel = new QSortFilterProxyModel(this);
    14. directoryProxyModel->setSourceModel(QFSMdirectoryView);
    15. directoryProxyModel->sort(0, Qt::AscendingOrder);
    16.  
    17. ui->directoryTreeView->setModel(directoryProxyModel);
    18. ui->directoryTreeView->setSortingEnabled(true);
    19. ui->directoryTreeView->setColumnHidden(1, true);
    20. ui->directoryTreeView->setColumnHidden(2, true);
    21. ui->directoryTreeView->setColumnHidden(3, true);
    22.  
    23. directorySelectionModel = new QItemSelectionModel(directoryProxyModel);
    24. ui->directoryTreeView->setSelectionModel(directorySelectionModel);
    25. connect(directorySelectionModel, SIGNAL(selectionChanged (const QItemSelection&, const QItemSelection&)), this, SLOT(directorySelectionChanged(QItemSelection,QItemSelection)));
    26.  
    27. QFSMdirectoryView->setRootPath(qsPath);
    28.  
    29. QFSMfilenameView = new QFileSystemModel(this);
    30. QFSMfilenameView->setFilter(QDir::Files | QDir::NoDotAndDotDot);
    31. ui->filenameListView->setModel(QFSMfilenameView);
    32.  
    33. QSIMframeView = new QStandardItemModel(this);
    34. ui->frameListView->setModel(QSIMframeView);
    35.  
    36. filenameSelectionModel = new QItemSelectionModel(QFSMfilenameView);
    37. ui->filenameListView->setSelectionModel(filenameSelectionModel);
    38. connect(filenameSelectionModel, SIGNAL(selectionChanged (const QItemSelection&, const QItemSelection&)), this, SLOT(filenameSelectionChanged(QItemSelection,QItemSelection)));
    39. }
    40.  
    41. MainWindow::~MainWindow()
    42. {
    43. delete ui;
    44. }
    45.  
    46. void MainWindow::on_actionExit_triggered()
    47. {
    48. close();
    49. }
    50.  
    51. void MainWindow::filenameSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
    52. {
    53. QSIMframeView->clear();
    54.  
    55. TagLib::String sfn = MyQStringToTString(QFSMfilenameView->filePath(filenameSelectionModel->currentIndex()));
    56.  
    57. TagLib::MPEG::File f(sfn.toCString());
    58.  
    59. TagLib::ID3v2::Tag *id3v2tag = f.ID3v2Tag();
    60. if(id3v2tag)
    61. {
    62. for(TagLib::ID3v2::FrameList::ConstIterator it = id3v2tag->frameList().begin(); it != id3v2tag->frameList().end(); ++it)
    63. {
    64. TagLib::String fid = (*it)->frameID();
    65. QStandardItem *child = new QStandardItem(QString("%1 : %2").arg(TStringToQString(fid)).arg(TStringToQString((*it)->toString())));
    66. QSIMframeView->appendRow(child);
    67. }
    68. } else {
    69. QStandardItem *child = new QStandardItem(QString("ERROR: No valid tag!"));
    70. QSIMframeView->appendRow(child);
    71. }
    72. }
    73.  
    74. void MainWindow::directorySelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
    75. {
    76.  
    77. qDebug("directoySelectionChanged fired.");
    78.  
    79. // QString qsPath = QFSMdirectoryView->fileInfo(directorySelectionModel->currentIndex()).absoluteFilePath();
    80. // ui->filenameListView->setRootIndex(QFSMfilenameView->setRootPath(qsPath));
    81. }
    To copy to clipboard, switch view to plain text mode 

    Thanks all

    -Enjoy
    fh : )_~

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QFileSystemModel, QItemSelectionModel, QSortFilterProxyModel

    The indexes in the selection are now from the proxy model, the directory model doesn't understand them.

    You have two options:
    1) use the mapToSource() function of the proxy model to get an index that the source model understands
    2) retrieve the data directly through the index using the data() method of QModelIndex and the item role for the data aspect you want (in your case most likely QFileSystemModel::FilePathRole)

    Cheers,
    _

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

    Festus Hagen (9th July 2013)

  4. #3
    Join Date
    Jul 2013
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QFileSystemModel, QItemSelectionModel, QSortFilterProxyModel

    Yea, I understood the proxy took over, I had tried both ways, got nowhere with MapToSource, Docs confused me. With the data() method all I could get back was the un-qualified directory name (ie: no path), And that is because I didn't have the missing key: QFileSystemModel::FilePathRole!

    Thank you!

    The fixed directorySelectionChanged:
    Qt Code:
    1. void MainWindow::directorySelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
    2. {
    3. ui->filenameListView->setRootIndex(QFSMfilenameView->setRootPath(directorySortFilterModel->data(selected.indexes()[0], QFileSystemModel::FilePathRole).toString()));
    4. }
    To copy to clipboard, switch view to plain text mode 

    Think I'm going to change selectionChanged(...) to currentChanged(...) and use QModelIndex versus QItemSelection ...

    Thanks

    -Enjoy
    fh : )_~

Similar Threads

  1. Replies: 3
    Last Post: 22nd August 2012, 03:32
  2. Replies: 0
    Last Post: 9th March 2011, 00:08
  3. QItemSelectionModel and QTreeView
    By rrafluap in forum Qt Programming
    Replies: 0
    Last Post: 13th December 2010, 19:49
  4. Replies: 2
    Last Post: 18th November 2009, 23:14
  5. Selections in a TableView with a QItemSelectionModel
    By xelag in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2006, 09:45

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.