QStringListModel的简单用法

  参考<<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中的内容进行增加、编辑和移除。

                                    

posted @ 2012-10-17 19:45  venow  阅读(8329)  评论(0编辑  收藏  举报