一步步学Qt,第五天-Qt这样的小小差异你发现了多少
一步步学Qt,第5天-Qt这样的小小差异你发现了多少
今天下了一个小小得程序,一个小小的文件目录浏览器。在看到这个地方的时候,看到了Qt你们的Model/View模式,原本小弟疑问这个玩意没有什么模式,自己借用其他的设计模式应该可以的。没想到Qt自己有自己的一套模式。不多说看程序:
#include <QDirModel> #include <QTreeView> #include <QTableView> #include <QListView> #include <QApplication> #include <QSplitter> #include <QAbstractItemModel> #include <QAbstractItemView> int main(int argc,char *argv[]){ QApplication a(argc,argv); QDirModel model; QListView list; QTreeView tree; QTableView table; tree.setModel(&model); list.setModel(&model); table.setModel(&model); tree.setSelectionMode(QAbstractItemView::MultiSelection); list.setSelectionMode(tree.selectionMode()); table.setSelectionMode(tree.selectionMode()); QObject::connect(&tree,SIGNAL(doubleClicked(QModelIndex)),&list, SLOT(setRootIndex(QModelIndex))); QObject::connect(&tree,SIGNAL(doubleClicked(QModelIndex)),&table, SLOT(setRootIndex(QModelIndex))); QSplitter *splitter = new QSplitter; splitter->addWidget(&tree); splitter->addWidget(&list); splitter->addWidget(&table); splitter->setWindowTitle(QObject::tr("Model/View")); splitter->show(); return a.exec(); }
运行效果:
注意下面的代码:
tree.setSelectionMode(QAbstractItemView::MultiSelection); list.setSelectionMode(tree.selectionMode()); table.setSelectionMode(tree.selectionMode());
我在Linux窗口程序设计-Qt4精彩实例上面看到的是
tree.setSelectionModel(QAbstractItemView::MultiSelection); list.setSelectionModel(tree.selectionMode()); table.setSelectionModel(tree.selectionMode());
错误提示如下:
../FileDirectoryView/main.cpp: In function 'int qMain(int, char**)':
../FileDirectoryView/main.cpp:23: error: no matching function for call to 'QListView::setSelectionModel(QAbstractItemView::SelectionMode)'
d:/StudyAppsInstall/QtSDK/Desktop/Qt/4.7.3/mingw/include/QtGui/qabstractitemview.h:132: note: candidates are: virtual void QAbstractItemView::setSelectionModel(QItemSelectionModel*)
../FileDirectoryView/main.cpp:24: error: no matching function for call to 'QTableView::setSelectionModel(QAbstractItemView::SelectionMode)'
d:/StudyAppsInstall/QtSDK/Desktop/Qt/4.7.3/mingw/include/QtGui/qtableview.h:73: note: candidates are: virtual void QTableView::setSelectionModel(QItemSelectionModel*)
可以看出是因为类型不匹配问题:no matching function for call to 'QTableView::setSelectionModel(QAbstractItemView::SelectionMode)'
查看qt的帮助文档:
首先查看QTreeView的setSelectionModel方法
void QTreeView::setSelectionModel ( QItemSelectionModel *selectionModel ) [virtual]
Reimplemented from QAbstractItemView::setSelectionModel().
看到是重新实现了 QAbstractItemView::setSelectionModel().那么查看QAbstractItemView的setSelectionModel方法在
QAbstractItemView Class Reference
中查看setSelectionModel方法:
void | setSelectionMode ( QAbstractItemView::SelectionModemode ) |
virtual void | setSelectionModel ( QItemSelectionModel * selectionModel ) |
出书的人啊,不可以误导人的哦。