QT5.9 c++ 第5章:
接着我们来看第二个模块程序,QStirngListModel 的使用,本文的例子是书上的例子,有些地方自己修改了,不多废话,一起来看看。
首先是ui画面的布局方式,本例子窗口从QWidget继承过来的类Widget, 布局方式如下图:
按钮命名
恢复列表 | btnIniList | QListView | listView |
添加项 | btnListAppend | 文本清空 | btnTextClear |
插入项 | btnListInsert | 显示数据... | btnTextImport |
删除当前项 | btnListDelete | QplainTextEdit | plainTextEdit |
清除列表 | btnListClear | QLabel | LabInfo |
首先是 widget.h 中的代码部分。
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QStringListModel>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
private:
QStringListModel * theMode;
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_btnListAppend_clicked();
void on_btnListInsert_clicked();
void on_btnIniList_clicked();
void on_btnListDelete_clicked();
void on_btnListClear_clicked();
void on_listView_clicked(const QModelIndex &index);
void on_btnTextClear_clicked();
void on_btnTextImport_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
首先,在Widget类中定义:
QStringListModel * theMode;
同时需要添加头文件
#include <QStringListModel>
槽部分,分别点击按钮右键,转到槽,选择对应的函数。下面来看看对应cpp 功能代码部分吧。
首先是构造函数部分:
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QStringList theStrList;//保存初始 StringList
theStrList<<"北京"<<"上海"<<"天津"<<"河北"<<"山东"<<"四川"<<"重庆"<<"广东"<<"河南"; //初始化 StringList
theMode = new QStringListModel(this);
theMode->setStringList(theStrList);
ui->listView->setModel(theMode);
ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked|
QAbstractItemView::SelectedClicked);
}
首先 QStringListModel 的 setStringList() 函数将一个字符串列表内容存放进去。
在用QlistView 中setModel()来设置界面的模型,使得相互关联。setEditTriggers() 函数编写状态。
QAbstractItemView::DoubleClicked |QAbstractItemView::SelectedClicked 双击选中,就可以对listView进行数据编写。
恢复列表 按钮对应的槽函数代码为:
void Widget::on_btnIniList_clicked()
{
//重新载入theStrList的内容,初始化theModel的内容
QStringList theStrList; //保存初始 StringList
theStrList<<"北京"<<"上海"<<"天津"<<"河北"<<"山东"<<"四川"<<"重庆"<<"广东"<<"河南"; //初始化 StringList
theMode->setStringList(theStrList);
}
添加项 按钮对应的槽函数代码为:
void Widget::on_btnListAppend_clicked()
{
theMode->insertRow(theMode->rowCount());//在尾部插入一空行
QModelIndex index = theMode->index(theMode->rowCount()-1,0);//获取最后一行
theMode->setData(index,"new item",Qt::DisplayRole);//设置显示文字
ui->listView->setCurrentIndex(index);//设置当前选中的行
}
插入代码 按钮对应的槽函数代码为:
void Widget::on_btnListInsert_clicked()
{
QModelIndex index = ui->listView->currentIndex();
theMode->insertRow(index.row());//插入当前行
theMode->setData(index,"insert item",Qt::DisplayRole);//设置显示文字
ui->listView->setCurrentIndex(index);//设置当前选中的行
}
删除当前项 按钮对应的槽函数代码为:
void Widget::on_btnListDelete_clicked()
{
QModelIndex index = ui->listView->currentIndex(); //获取当前的编号
theMode->removeRow(index.row());//删除选择的那一行
}
清除列表 按钮对应的槽函数代码为:
void Widget::on_btnListClear_clicked()
{
theMode->removeRows(0,theMode->rowCount());
}
列表 对应的槽函数代码为:
void Widget::on_listView_clicked(const QModelIndex &index)
{
ui->LabInfo->setText(QString::asprintf("当前项:row=%d, column=%d",index.row(),index.column()));
}
清空文本 对应的槽函数代码为:
void Widget::on_btnTextClear_clicked()
{
ui->plainTextEdit->clear();
}
显示数据模型的StringList 对应的槽函数代码为:
void Widget::on_btnTextImport_clicked()
{
QStringList tmpList;
tmpList = theMode->stringList();
ui->plainTextEdit->clear(); //文本框清空
for (int i =0;i<tmpList.count();i++) {
ui->plainTextEdit->appendPlainText(tmpList.at(i));
}
}
这样就完成全部的功能,代码例子已经上传,可以下载,有问题请留言,共同学习与进步。