Hi all,

WinXPsp3
Qt5.0.2 and Qt Creator 2.7.0

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.
I'm it it to learn, so if there is a better way ...

The goal:
To have a sorted directory only TreeView on the left, the files of the currently selected directory are listed (filtered and sorted) in the ListView (or TableView) in the middle, the elements of the currently selected file are listed in the (yet to be added) TableView to the right.

Info:
A TreeView populated with QFileSystemModel wrapped with a QSortFilterProxyModel purely for sort, no filters ... There is also a QItemSelectionModel attached!
A ListView populated with QFileSystemModel wrapped with a QSortFilterProxyModel for sort and filter purposes ... There is also a QItemSelectionModel attached.

Question 1:
Once the proxyModel is attached, how does one setRootPath(...) of the under laying QFileSystemModel.
I also need the QModelIndex returned from that setRootPath(...) call as well.

See commented code at end of .cpp to see how I had previously done it prior to the proxyModel.

Have tried many things, to many to list all (besides I'm sure I've forgotten more then I remember)
Like casting the proxies sourceModel(...) as I've seen others do in my searches.

Question 2:
Maybe my whole design is flawed?

For the ui:
TreeView named directoryTreeView
ListView named filenameListView

The code:
Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QFileSystemModel>
  6. #include <QItemSelectionModel>
  7. #include <QSortFilterProxyModel>
  8. #include <QtDebug>
  9.  
  10. namespace Ui {
  11. class MainWindow;
  12. }
  13.  
  14. class MainWindow : public QMainWindow
  15. {
  16. Q_OBJECT
  17.  
  18. public:
  19. explicit MainWindow(QWidget *parent = 0);
  20. ~MainWindow();
  21.  
  22. private slots:
  23. void filenameSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
  24.  
  25. void directorySelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
  26.  
  27. private:
  28. Ui::MainWindow *ui;
  29. // directory Tree View
  30. QFileSystemModel *QFSMdirectoryView;
  31. QItemSelectionModel *directorySelectionModel;
  32. QSortFilterProxyModel *directorySortFilterModel;
  33. // filename List View
  34. QFileSystemModel *QFSMfilenameView;
  35. QItemSelectionModel *filenameSelectionModel;
  36. QSortFilterProxyModel *filenameSortFilterModel;
  37. };
  38.  
  39. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #include "MainWindow.h"
  2. #include "ui_MainWindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9.  
  10. QString qsPath = QDir::currentPath();
  11.  
  12. // directory Tree View
  13. QFSMdirectoryView = new QFileSystemModel(this);
  14. QFSMdirectoryView->setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
  15.  
  16. directorySortFilterModel = new QSortFilterProxyModel(this);
  17. directorySortFilterModel->setSourceModel(QFSMdirectoryView);
  18. directorySortFilterModel->sort(0, Qt::AscendingOrder);
  19.  
  20. ui->directoryTreeView->setModel(directorySortFilterModel);
  21. ui->directoryTreeView->setSortingEnabled(true);
  22. ui->directoryTreeView->setColumnHidden(1, true);
  23. ui->directoryTreeView->setColumnHidden(2, true);
  24. ui->directoryTreeView->setColumnHidden(3, true);
  25.  
  26. directorySelectionModel = new QItemSelectionModel(directorySortFilterModel);
  27. ui->directoryTreeView->setSelectionModel(directorySelectionModel);
  28. connect(directorySelectionModel, SIGNAL(selectionChanged (const QItemSelection&, const QItemSelection&)), this, SLOT(directorySelectionChanged(QItemSelection,QItemSelection)));
  29.  
  30. QFSMdirectoryView->setRootPath(qsPath);
  31.  
  32. // filename List View
  33. QFSMfilenameView = new QFileSystemModel(this);
  34. QFSMfilenameView->setFilter(QDir::Files | QDir::NoDotAndDotDot);
  35.  
  36. filenameSortFilterModel = new QSortFilterProxyModel(this);
  37. filenameSortFilterModel->setSourceModel(QFSMfilenameView);
  38. filenameSortFilterModel->sort(0, Qt::AscendingOrder);
  39.  
  40. ui->filenameListView->setModel(filenameSortFilterModel);
  41.  
  42. filenameSelectionModel = new QItemSelectionModel(filenameSortFilterModel);
  43. ui->filenameListView->setSelectionModel(filenameSelectionModel);
  44. connect(filenameSelectionModel, SIGNAL(selectionChanged (const QItemSelection&, const QItemSelection&)), this, SLOT(filenameSelectionChanged(QItemSelection,QItemSelection)));
  45. }
  46.  
  47. MainWindow::~MainWindow()
  48. {
  49. delete ui;
  50. }
  51.  
  52. void MainWindow::filenameSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
  53. {
  54. QString qsPath = QFSMfilenameView->filePath(selected.indexes()[0]);
  55. qDebug() << "Selected filename: " << qsPath;
  56. }
  57.  
  58. void MainWindow::directorySelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
  59. {
  60. QString qsPath = directorySortFilterModel->data(selected.indexes()[0], QFileSystemModel::FilePathRole).toString();
  61.  
  62. // Obviously this no longer works with the proxy ...
  63. // QModelIndex index = QFSMfilenameView->setRootPath(qsPath);
  64. // ui->filenameListView->setRootIndex(index);
  65. }
To copy to clipboard, switch view to plain text mode 
-Enjoy
fh : )_~