当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
网站自动翻译-网站批量自动翻译-网站免费翻译导出
CCF paper conference IEEE how to query all articles of a conference journal
pyqt5连接MYSQL数据库问题
10份重磅报告 — 展望中国数字经济未来
Create an application operation process using the kubesphere GUI
受邀出席Rust开发者大会|Rust如何助力量化高频交易?
企业级数据治理工作怎么开展?Datahub这样做
【项目管理技术的优势】
Camera Hal OEM模块 ---- cmr_snapshot.c
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一
Crack detection technology based on deep learning
借小程序容器打造自有App小程序生态
Running yum reports Error: Cannot retrieve metalink for reposit
go语言的接口
WPF 实现窗体抖动效果
19、商品微服务-srv层实现
使用kubesphere图形界面创建一个应用操作流程
Golang map数组按字段分类
Oracle 19c配置ob server
SQL 数据更新









