1 #include "mainwindow.h" 2 #include <QApplication> 3 #include <QAbstractItemModel> 4 #include <QAbstractItemView> 5 #include <QItemSelectionModel> 6 7 #include <QDirModel> 8 #include <QTreeView> 9 #include <QListView> 10 #include <QTableView> 11 #include <QSplitter> 12 13 int main(int argc, char *argv[]) 14 { 15 QApplication a(argc, argv); 16 17 QDirModel model;//模型 18 19 QTreeView treeview;//视图(共享模型) 20 QListView listview; 21 QTableView tableview; 22 23 treeview.setModel(&model); 24 treeview.setSelectionMode(QAbstractItemView::MultiSelection); 25 26 listview.setModel(&model); 27 listview.setSelectionModel(treeview.selectionModel()); 28 29 tableview.setModel(&model); 30 tableview.setSelectionModel(treeview.selectionModel()); 31 32 QSplitter *splitter = new QSplitter; 33 splitter->addWidget(&treeview); 34 splitter->addWidget(&listview); 35 splitter->addWidget(&tableview); 36 37 splitter->show(); 38 39 QObject::connect(&treeview,SIGNAL(doubleClicked(QModelIndex)),&listview,SLOT(setRootIndex(QModelIndex))); 40 QObject::connect(&treeview,SIGNAL(doubleClicked(QModelIndex)),&tableview,SLOT(setRootIndex(QModelIndex))); 41 42 return a.exec(); 43 }