当前位置:网站首页>Qt之QListView的简单使用(含源码+注释)
Qt之QListView的简单使用(含源码+注释)
2022-06-30 05:29:00 【lw只吃亿点.】
一、QTreeView操作示例图
1.节点的添加删除示例图
下图为节点添加删除示例图;源码在本文第三节(源码含详细注释)。
2.节点的值的获取与修改
下图为节点对节点值的操作,其中包含获取值、设置值等;源码在本文第三节(源码含详细注释)。
提示:不会使用Qt设计师设计界面的小伙伴点击这里
二、QListView(个人理解)
同样我们将QListView和QTableView、QTreeView做对比
- 三者者都是类似MVC(Model View Controller)模式,其中都包含Delegate,请查看Qt代理的实现(按钮篇)、Qt代理的实现(常规控件篇);
- QListView与其他两个View使用的数据模型不同,QListView使用的是QStringListModel存放数据;
- QListView也存在一个继承其QListWidget的子类;
- QListView因为使用的数据模型不同不能像其他两个View可以单独获取Item进行设置(要设置值需要使用setData函数进行设置);
- QListView的操作函数相较于其他两个View视图的操作函数更少;
- QListView在我个人使用时貌似不能设置第二列。
三、源码
CMainWindow.h
#ifndef CMAINWINDOW_H
#define CMAINWINDOW_H
#include <QMainWindow>
#include <QStringListModel> //数据模型类
namespace Ui {
class CMainWindow;
}
class CMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit CMainWindow(QWidget *parent = nullptr);
~CMainWindow();
private slots:
/** * @brief on_removeNodeBtn_clicked 删除按钮槽函数 */
void on_removeNodeBtn_clicked();
/** * @brief on_addNodeBtn_clicked 添加按钮槽函数 */
void on_addNodeBtn_clicked();
/** * @brief on_getNodeValBtn_clicked 获取值按钮槽函数 */
void on_getNodeValBtn_clicked();
/** * @brief on_setNodeValBtn_clicked 设置值按钮槽函数 */
void on_setNodeValBtn_clicked();
private:
Ui::CMainWindow *ui;
QStringListModel *m_pModel; //数据模型对象指针
};
#endif // CMAINWINDOW_H
CMainWindow.cpp
#include "CMainWindow.h"
#include "ui_CMainWindow.h"
CMainWindow::CMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CMainWindow)
{
ui->setupUi(this);
//设置窗口标题
this->setWindowTitle("QListView的简单使用");
//===============数据模型(QStandardItemModel)===============
//建立字符串列表模型对象空间并指定父对象
m_pModel = new QStringListModel(ui->listView);
//将字符串列表模型设置到树形视图上
ui->listView->setModel(m_pModel);
//添加默认值
m_pModel->setStringList(QStringList()
<< "第一行"
<< "第二行"
<< "第三行");
}
CMainWindow::~CMainWindow()
{
//! 析构函数:
//! 有些小伙伴会发现我没有析构model对象,
//! 那是因为我在获取对象空间的时候指定了父对象,
//! 当其父对象析构时,会先析构其子对象为指针的对象。
delete ui;
}
void CMainWindow::on_removeNodeBtn_clicked()
{
//通过当前位置的index拿到当前行
int row = ui->listView->currentIndex().row();
//若当前行为-1则没有任何选中,然后返回
if( -1 == row)
{
return;
}
//直接移除当前行
m_pModel->removeRow(row);
}
void CMainWindow::on_addNodeBtn_clicked()
{
//获取新建节点要设置的文本
QString str = ui->nodeValEdit->text();
//若节点值为空则不能添加直接返回
if(str.isEmpty())
{
return;
}
//! 使用insert插入行
//! (因为没有append函数,所以要单独获取index设置值)
//所添加行的位置
int row = m_pModel->rowCount();
//插入行
m_pModel->insertRow(row);
//获取刚刚插入行的index
QModelIndex index = m_pModel->index(row);
//通过setData函数
m_pModel->setData(index, str);
//将新添加的行设置为当前行
ui->listView->setCurrentIndex(index);
}
void CMainWindow::on_getNodeValBtn_clicked()
{
//获取当前的index
QModelIndex index = ui->listView->currentIndex();
//若当前行为-1则没有任何选中,然后返回
if( -1 == index.row())
{
return;
}
//! 将当前位置上的值设置到编辑框中
//! data函数的参数默认值为Qt::DisplayRole,在个人获取显示值时可不传参
ui->nodeValEdit->setText(index.data(Qt::DisplayRole).toString());
}
void CMainWindow::on_setNodeValBtn_clicked()
{
//获取当前的index
QModelIndex index = ui->listView->currentIndex();
//获取节点要设置的文本
QString str = ui->nodeValEdit->text();
//若当前行为-1或者设置的字符串为空则返回
if( -1 == index.row() || str.isEmpty())
{
return;
}
//将获取的值设置到当前index位置上
m_pModel->setData(index, str);
}
总结
QListView目前在我实际应用中是比较少的,所以用多了其他两个View突然使用QListView会有一点不习惯,最主要是因为QListView和其他其他两个View使用的数据模型对象不同的原因,目前我的大概问题就这些了。
就到这里吧,晚安咯…
相关文章
Qt之QTableView的简单使用(含源码+注释)
Qt之QTreeView的简单使用(含源码+注释)
Qt代理的实现(按钮篇,含源码+注释)
Qt代理的实现(常规控件篇,含源码+注释)
Qt之QTableView设置多列表头复选框(自定义QHeaderView)、单元格复选框(含源码+注释)
Qt之QSortFilterProxyModel的简单使用(QTableView搜索功能,含源码+注释)
友情提示——哪里看不懂可私哦,让我们一起互相进步吧
(创作不易,请留下一个免费的赞叭 谢谢 ^o^/)
注:文章为作者编程过程中所遇到的问题和总结,内容仅供参考,若有错误欢迎指出。
注:如有侵权,请联系作者删除
边栏推荐
- GoLand No Tests Were Run : 不能使用 fmt.Printf() &lt;BUG&gt;
- Revit二次开发---未打开项目使用面板功能
- 14x1.5cm竖向标签有点难,VFP调用BarTender来打印
- [notes] unity webgl input Chinese
- Shopping list--
- unity 扫描圈 圆扩展方法
- Unity publishing /build settings
- Very nervous. What should I do on the first day of software testing?
- Nestjs introduction and environment construction
- Database SQL language 06 single line function
猜你喜欢

【LeetCode】Easy | 225. Using queue to realize stack (pure C manual tearing queue)

抓取手机端变体组合思路设想

9. naive Bayes

mmcv常用API介绍

Records of some problems encountered during unity development (continuously updated)

AI大模型落地大考,浪潮交出了怎样的答卷?

Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation

OpenCL线程代数库ViennaCL的使用

Intellj idea jars projects containing external lib to other project reference methods - jars

The file has been downloaded incorrectly!
随机推荐
Word frequency statistics (string, list)
14x1.5cm vertical label is a little difficult, VFP calls bartender to print
Unity scroll view element drag and drop to automatically adsorb centering and card effect
Stack overflow caused by C # using protobuf stack overflow
Intellj idea jars projects containing external lib to other project reference methods - jars
如何写论文
UML tools
Pyinstaller flash back
Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
Xijiao 21 autumn "motor and drive" online homework answer sheet (III) [standard answer]
Redistemplate common method summary
[Blue Bridge Road -- bug free code] analysis of AT24C02 storage code
Promise知识点拾遗
Unity3d learning notes-1 (C # learning)
Very nervous. What should I do on the first day of software testing?
Unity call Exe program
Digital signature——
Bessel curve with n control points
GoLand No Tests Were Run : 不能使用 fmt.Printf() &lt;BUG&gt;
Gradient clip in dqn