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手册。
下边给出例子,照例,我们先给出效果:
![](https://img-blog.csdn.net/20130520181659786)
整个例子用了一个QListView,一个QStirngListModel和一个QDialogButtonBox,构造函数接收一个存储着QString的QStringList用来初始化QStringListModel,然后用QListView的setModel()函数把数据和model联系起来。这里我们可以很清楚的看到MVC架构的各个部分是如何关联起来的:QStringListModel是一组数据集,存储的是一系列字符串数据;QListView是视图类,是一个窗口部件,用来观察这组数据;一些和它关联的操作,我们统称为控制(insert,delete等) 。下面给出代码:
-
- #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
-
-
- #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()
- {
- }
-
-
- #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();
- }