当前位置:网站首页>QListView的使用
QListView的使用
2022-08-02 11:38:00 【物随心转】
一、介绍
QListView可以用来以列表的形式展示数据,在Qt中使用model/View结构来管理数据与视图的关系,model负责数据的存取,数据的交互则通过delegate来实现。
二、model
QT提供了一些现成的models用于处理数据项:
QStringListModel 用于存储简单的QString列表。
QStandardItemModel 管理复杂的树型结构数据项,每项都可以包含任意数据。
QDirModel 提供本地文件系统中的文件与目录信息。
QSqlQueryModel 对SQL的查询结果集进行封装
QSqlTableModel 对SQL中的table进行封装。
QSortFilterProxyModel 对另一个model执行sort and/or filter
model中存放的每项数据都有相应的"model index",由QModelIndex类来表示。每个index由三个部分构成:row,column和表明所属model的指针。对于一维的list model,column部分永远为0。
三、实例
这里分别使用QStringListModel与QStandardItemModel 展示了ListView显示数据。
2.1 首先,使用qtcreator创建一个widget项目,各种设置一律采用默认。

2.2 打开项目中的界面文件,然后我们在界面中拖入一个listview控件。
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
边栏推荐
- Oracle 单实例19.11升级到19.12
- 小程序插件的生态丰富,加速开发建设效率
- Excel dynamic chart production
- Jest 测试框架 beforeEach 的设计原理解析
- 小程序插件让开发者受益的几个理由
- Crack detection technology based on deep learning
- 借小程序容器打造自有App小程序生态
- 华为eNSP(基础实验通信)
- 21 Days Learning Challenge - Day 1 Punch (Screen Density)
- Create your own app applet ecosystem with applet containers
猜你喜欢

【kali-信息收集】(1.9)Metasploit+搜索引擎工具Shodan

Camera Hal OEM模块 ---- cmr_snapshot.c

华为eNSP(基础实验通信)

Challenge LeetCode1000 questions in 365 days - Day 047 Design Circular Queue Circular Queue

AlphaFold又放大招,剑指整个生物界!

Create a devops CI/CD process using the kubesphere GUI

当POC遇见RPA:RPA项目顺利实施的关键

爆款视频怎么做?这里或许有答案!

How to connect TDengine through DBeaver?

Create an application operation process using the kubesphere GUI
随机推荐
Challenge LeetCode1000 questions in 365 days - Day 047 Design Circular Queue Circular Queue
8大软件供应链攻击事件概述
深度学习100例 —— 卷积神经网络(CNN)实现mnist手写数字识别
10份重磅报告 — 展望中国数字经济未来
微信小程序---组件开发与使用
go语言的接口
ssm网页访问数据库数据报错
Several reasons why applet plugins benefit developers
The exchange - string dp
jvmxmx和xms参数分析(设定优化校准)
AlphaFold又放大招,剑指整个生物界!
Deep Learning 100 Examples - Convolutional Neural Network (CNN) for mnist handwritten digit recognition
基于深度学习的裂缝检测技术
暑期总结3
解决anaconda下载pytorch速度极慢的方法
SQL函数 $TRANSLATE
ansible模块--copy模块
jacoco的学习以及理解
ssm web page access database data error
AdguardHome如何配置设置?我的AdguardHome配置内容过滤器拦截列表