当前位置:网站首页>The use of QListView
The use of QListView
2022-08-02 11:46:00 【turn things around】
一、介绍
QListViewCan be used to display data in the form of a list,在Qt中使用model/View结构来管理数据与视图的关系,model负责数据的存取,The interaction of data is passeddelegate来实现.
二、model
QT提供了一些现成的models用于处理数据项:
QStringListModel 用于存储简单的QString列表.
QStandardItemModel 管理复杂的树型结构数据项,每项都可以包含任意数据.
QDirModel 提供本地文件系统中的文件与目录信息.
QSqlQueryModel 对SQL的查询结果集进行封装
QSqlTableModel 对SQL中的table进行封装.
QSortFilterProxyModel 对另一个model执行sort and/or filter
modelEach item of data stored in "model index",由QModelIndex类来表示.每个index由三个部分构成:row,columnand indicate belongingmodel的指针.对于一维的list model,columnpart is always0.
三、实例
这里分别使用QStringListModel与QStandardItemModel 展示了ListView显示数据.
2.1 首先,使用qtcreator创建一个widget项目,All settings are defaulted.
2.2 Open the interface file in the project,Then we drag and drop one into the interfacelistview控件.
2.3 然后,打开mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStringListModel>
#include <QStandardItemModel>
#include <QModelIndex>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
public:
QStringListModel *Model;
QStandardItemModel *ItemModel;
void init();
private slots:
void showClick(QModelIndex index);
};
#endif // MAINWINDOW_H
打开mainwindow.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
init();
}
void MainWindow::init()
{
// QStringListModel的使用
// QStringList strlist;
// strlist<<"A"<<"B"<<"C"<<"D";
// Model = new QStringListModel(strlist);
// ui->listView->setModel(Model);
// Model = new QStringListModel(this);
// Model->setStringList(QStringList()<<"语文"<<"数学"<<"英语");
// ui->listView->setModel(Model);
ItemModel = new QStandardItemModel(this);
QStringList strList; // 需要展示的数据
strList.append("A");
strList.append("B");
strList.append("C");
strList.append("D");
strList.append("E");
strList.append("F");
strList.append("G");
int nCount = strList.size();
for(int i = 0; i < nCount; i++)
{
QString string = static_cast<QString>(strList.at(i));
QStandardItem *item = new QStandardItem(string);
ItemModel->appendRow(item);
}
ui->listView->setModel(ItemModel); // listview设置Model
ui->listView->setFixedSize(200,300);
// 绑定事件
connect(ui->listView,SIGNAL(clicked(QModelIndex)),this,SLOT(showClick(QModelIndex)));
}
void MainWindow::showClick(QModelIndex index)
{
QString strTemp;
strTemp = index.data().toString();
QMessageBox msg;
msg.setText(strTemp);
msg.exec();
}
MainWindow::~MainWindow()
{
delete ui;
}
运行结果
参考:
QT--QlistView 简单应用_yifanmoon的博客-CSDN博客_qlistview
边栏推荐
猜你喜欢
随机推荐
【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
npm WARN deprecated [email protected] This version of tar is no longer supported, and will not receive
What is the future of smartwatches?
SQL 数据更新
使用kubesphere图形界面创建一个devops的CI/CD流程
翁恺C语言程序设计网课笔记合集
Oracle 19c 连接PDB
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
CCF paper conference IEEE how to query all articles of a conference journal
Problem solving in the process of using mosquitto
“纯C”实现——三子棋小游戏
记录代码
Create a devops CI/CD process using the kubesphere GUI
力扣35-搜索插入位置——二分查找
云原生(三十) | Kubernetes篇之应用商店-Helm介绍
Oracle 19c配置ob server
细学常用类,集合类,IO流
您应该知道的 Google Sheets 使用技巧
故障分析 | 一条 SELECT 语句跑崩了 MySQL ,怎么回事?
MapStruct