參考<<C++ GUI Programming with Qt 4>>中文版第二版中的例子"TeamLeaderDialog",簡單介紹QStringListModel的用法,說白了,QStringListModel就是封裝了QStringList的model。QStringList是一種很常用的數據類型,它實際上是一個字符串列表。我們用QListView作為視圖。對QStringListModel的修改都會實時的反應到視圖QListView中。
teamleaderdialog.h文件:
#ifndef TEAMLEADERDIALOG_H #define TEAMLEADERDIALOG_H #include <QtGui/QDialog> #include <QListView> #include <QStringListModel> class TeamLeaderDialog : public QDialog { Q_OBJECT public: TeamLeaderDialog(const QStringList &leaders, QWidget *parent = 0); ~TeamLeaderDialog(); private slots: void insertName(); void deleteName(); void editName(); private: QListView *listView; QStringListModel *model; }; #endif // TEAMLEADERDIALOG_H
teamleaderdialog.cpp文件:
#include "teamleaderdialog.h" #include <QPushButton> #include <QHBoxLayout> #include <QVBoxLayout> #include <QInputDialog> TeamLeaderDialog::TeamLeaderDialog(const QStringList &leaders, QWidget *parent) : QDialog(parent) { model = new QStringListModel; model->setStringList(leaders); listView = new QListView; listView->setModel(model); QPushButton *insertButton = new QPushButton("Insert"); QPushButton *deleteButton = new QPushButton("Delete"); QPushButton *editButton = new QPushButton("Edit"); QHBoxLayout *hlayout = new QHBoxLayout; hlayout->addWidget(insertButton); hlayout->addWidget(deleteButton); hlayout->addWidget(editButton); QVBoxLayout *vlayout = new QVBoxLayout; vlayout->addWidget(listView); vlayout->addLayout(hlayout); setLayout(vlayout); connect(insertButton, SIGNAL(clicked()), this, SLOT(insertName())); connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteName())); connect(editButton, SIGNAL(clicked()), this, SLOT(editName())); } TeamLeaderDialog::~TeamLeaderDialog() { } void TeamLeaderDialog::insertName() { bool ok; QString name = QInputDialog::getText(this, tr("New Name"), tr(""), QLineEdit::Normal, tr(""), &ok); if (ok && !name.isEmpty()) { int row = listView->currentIndex().row(); model->insertRows(row, 1); QModelIndex index = model->index(row); model->setData(index, name); listView->setCurrentIndex(index); } } void TeamLeaderDialog::deleteName() { model->removeRows(listView->currentIndex().row(), 1); } void TeamLeaderDialog::editName() //直接按F2就可以編輯,不用自己實再實現編輯功能 { int row = listView->currentIndex().row(); QModelIndex index = model->index(row); QVariant variant = model->data(index, Qt::DisplayRole); //獲取當前選擇的項的文本 QString name = variant.toString(); bool ok; name = QInputDialog::getText(this, tr("Edit Name"), tr(""), QLineEdit::Normal, name, &ok); if (ok && !name.isEmpty()) { row = listView->currentIndex().row(); index = model->index(row); model->setData(index, name); listView->setCurrentIndex(index); } }
main.cpp文件:
#include <QtGui/QApplication> #include "teamleaderdialog.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QStringList leaders; leaders<<"Stooge Viller"<<"Littleface"<<"B-B Eyes"<<"Pruneface"<<"Mrs.Pruneface"<<"The Brow"<<"Vitamin Flintheart"; leaders<<"Flattop Sr"<<"Shakey"<<"Breathless Mahoney"<<"Mumbless"<<"Shoulders"<<"Sketch Paree"; TeamLeaderDialog w(leaders); w.show(); return a.exec(); }
程序運行界面如下,可以對ListView中的內容進行增加、編輯和移除。