当前位置:网站首页>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^/)
注:文章为作者编程过程中所遇到的问题和总结,内容仅供参考,若有错误欢迎指出。
注:如有侵权,请联系作者删除
边栏推荐
- Unity- the camera follows the player
- mmcv常用API介绍
- Use the code cloud publicholiday project to determine whether a day is a working day
- 炒美原油的国际交易平台如何能保障资金安全呢?
- Responsive flow layout
- Unity 3D model operation and UI conflict Scrollview
- Xiaosha's lunch
- Unity gets the resolution of the game view
- Network communication problem locating steps
- Configuration and use of controllers and routes in nestjs
猜你喜欢
Virtual and pure virtual destructions
遥感图像/UDA:Curriculum-Style Local-to-Global Adaptation for Cross-Domain Remote Sensing Image Segmentat
What are membrane stress and membrane strain
Unity- the camera follows the player
2022年,谁在推动音视频产业的新拐点?
Records of some problems encountered during unity development (continuously updated)
Unity screenshot method
强烈推荐十几款IDEA开发必备的插件
OpenCL线程代数库ViennaCL的使用
East Tower attack and defense world - XSS bypasses the safety dog
随机推荐
《谁动了我的奶酪》读后感
Delete the repeating elements in the sorting list (simple questions)
Uboot reads the DDR memory size by sending 'R' characters through the terminal
E: Topic focus
Database SQL language 05 SQL exercise
Introduction to Redux: initial experience of Redux
Vfpbs uploads excel and saves MSSQL to the database
Bessel curve with n control points
AI大模型落地大考,浪潮交出了怎样的答卷?
Responsive layout
[Blue Bridge Road -- bug free code] DS1302 time module code analysis
Unity limited time use limited trial time and use times
Access is denied encountered when vfpbs calls excel under IIS
Unity obtains serial port data
What are membrane stress and membrane strain
Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation
East Tower attack and defense world - XSS bypasses the safety dog
旋转框目标检测mmrotate v0.3.1入门
Another download address for typro
GoLand No Tests Were Run : 不能使用 fmt.Printf() &lt;BUG&gt;