QStringListModel的简单用法

参考<<C++ GUI Programming with Qt4>>中文版第二版中的例子"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中的内容进行增加、编辑和移除。

QStringListModel的简单用法
原文链接: https://www.cnblogs.com/venow/archive/2012/10/17/2728299.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/66123

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午12:10
下一篇 2023年2月9日 下午12:11

相关推荐