QListView是不顯示表頭和表框的,如果要顯示,可以使用QTreeView來
view的顯示屬性分為列表list顯示和icon圖標顯示,使用
QListView::setViewMode()來設置,默認為list型的
QListView::setModel ()用來設置view所關聯的model
獲取view中當前的位置,QListView::currentIndex(),返回的是QModelIndex類型的
設置當前行使用QListView::setCurrentIndex(QModelIndex),
設置某行可以編輯,使用QListView::edit(QModelIndex)
當某個QModelIndex被移動時候,信號indexesMoved(QModelIndexList)被發射
當雙擊某項時候,信號doubleClicked(QModelIndex)被發射
當大幾某項時候,信號clicked(QModelIndex)被發射
QStringListModel就是封裝了QStringList的model。QStringList是一種很常用的數據類型,它實際上是一個字符串列表。我們可以想象,對於一個list來說,如果提供一個字符串列表形式的數據,就應該能夠把這個數據展示出來。因為二者是一致的:QStringList是線性的,而list也是線性的。所以,QStringListModel很多時候都會作為QListView的model。
model使用insertRows,insertRow來添加多行或一行,使用setData()來設置該行的數據
rowCount()用來獲改model的行數
removeRows()和removeRow()用來刪除model中的一行或多行,詳見API手冊。
下邊給出例子,照例,我們先給出效果:
整個例子用了一個QListView,一個QStirngListModel和一個QDialogButtonBox,構造函數接收一個存儲着QString的QStringList用來初始化QStringListModel,然后用QListView的setModel()函數把數據和model聯系起來。這里我們可以很清楚的看到MVC架構的各個部分是如何關聯起來的:QStringListModel是一組數據集,存儲的是一系列字符串數據;QListView是視圖類,是一個窗口部件,用來觀察這組數據;一些和它關聯的操作,我們統稱為控制(insert,delete等) 。下面給出代碼:
//teamleadersdialog.h #ifndef TEAMLEADERSDIALOG_H #define TEAMLEADERSDIALOG_H #include <QDialog> #include <QStringListModel> #include <QListView> #include <QDialogButtonBox> class TeamLeadersDialog : public QDialog { Q_OBJECT public: TeamLeadersDialog(const QStringList &leaders, QWidget *parent = 0); ~TeamLeadersDialog(); private slots: void insert(); void del(); private: QStringListModel *model; QListView *listView; QDialogButtonBox *buttonBox; }; #endif // TEAMLEADERSDIALOG_H //teamleadersdialog.cpp #include <QPushButton> #include <QVBoxLayout> #include "teamleadersdialog.h" TeamLeadersDialog::TeamLeadersDialog(const QStringList &leaders, QWidget *parent) : QDialog(parent) { model = new QStringListModel(this); model->setStringList(leaders); listView = new QListView; listView->setModel(model); listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked); buttonBox = new QDialogButtonBox; QPushButton *insertButton = buttonBox->addButton( tr("&Insert"),QDialogButtonBox::ActionRole); QPushButton *deleteButton = buttonBox->addButton( tr("&Delete"), QDialogButtonBox::ActionRole); buttonBox->addButton(QDialogButtonBox::Ok); buttonBox->addButton(QDialogButtonBox::Cancel); connect(insertButton, SIGNAL(clicked()), this, SLOT(insert())); connect(deleteButton, SIGNAL(clicked()), this, SLOT(del())); connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(listView); layout->addWidget(buttonBox); setLayout(layout); setWindowTitle(tr("QStringListModel")); } void TeamLeadersDialog::insert() { int row = listView->currentIndex().row(); model->insertRows(row, 1); QModelIndex index = model->index(row); listView->setCurrentIndex(index); listView->edit(index); } void TeamLeadersDialog::del() { model->removeRows(listView->currentIndex().row(), 1); } TeamLeadersDialog::~TeamLeadersDialog() { } //main.cpp #include "teamleadersdialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); QStringList list; list << QObject::tr("xianziyu") << QObject::tr("yangwangming") << QObject::tr("yinshixin") << QObject::tr("baichengshuang") << QObject::tr("zhangzurui"); TeamLeadersDialog w(list); w.show(); return a.exec(); }