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

                                        

  • 相关阅读:
    i春秋——春秋争霸write up
    2017-2018-2 《网络对抗技术》20155322 Exp6 信息搜集与漏洞扫描
    2017-2018-2 《网络对抗技术》 20155322 Exp 5 MSF基础应用
    OpenCV学习系列(一) Mac下OpenCV + xcode人脸检测实现
    2017-2018-2 《网络对抗技术》 20155322 Exp4 恶意代码分析
    MacOS下netstat和lsof使用的若干问题
    OpenCV学习系列(零) Mac下OpenCV + xcode环境搭建
    2017-2018-2 《网络对抗技术》 20155322 Exp3 免杀原理与实践
    2017-2018-2 《网络对抗技术》 20155322 第五周 Exp2 后门原理与实践
    信息安全铁人三项赛--资质赛writeup
  • 原文地址:https://www.cnblogs.com/venow/p/2728299.html
Copyright © 2020-2023  润新知