当前位置:网站首页>Simple use of qlistview of QT (including source code + comments)
Simple use of qlistview of QT (including source code + comments)
2022-06-30 05:35:00 【LW only eats 100 million points】
One 、QTreeView Operation example diagram
1. Example of adding and deleting nodes
The following figure shows an example of adding and deleting nodes ; The source code is in the third section of this article ( The source code contains detailed comments ).
2. Get and modify the value of the node
The following figure shows the operation of nodes on node values , This includes getting values 、 Set values, etc ; The source code is in the third section of this article ( The source code contains detailed comments ).
Tips : Can't use Qt Designer design interface partners click here
Two 、QListView( Personal understanding )
Similarly, we will QListView and QTableView、QTreeView comparing
- All three are similar MVC(Model View Controller) Pattern , It all includes Delegate, Please check out Qt The realization of agent ( Button chapter )、Qt The realization of agent ( General controls );
- QListView With the other two View Different data models are used ,QListView It uses QStringListModel Storing data ;
- QListView There is also an inheritance QListWidget Subclasses of ;
- QListView Because the data model used is different, it can not be like the other two View It can be obtained separately Item Set it up ( To set the value, you need to use setData Function to set );
- QListView Compared with the other two View Views have fewer manipulation functions ;
- QListView It seems that the second column cannot be set for my personal use .
3、 ... and 、 Source code
CMainWindow.h
#ifndef CMAINWINDOW_H
#define CMAINWINDOW_H
#include <QMainWindow>
#include <QStringListModel> // Data model class
namespace Ui {
class CMainWindow;
}
class CMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit CMainWindow(QWidget *parent = nullptr);
~CMainWindow();
private slots:
/** * @brief on_removeNodeBtn_clicked Delete button slot function */
void on_removeNodeBtn_clicked();
/** * @brief on_addNodeBtn_clicked Add button slot function */
void on_addNodeBtn_clicked();
/** * @brief on_getNodeValBtn_clicked Get value button slot function */
void on_getNodeValBtn_clicked();
/** * @brief on_setNodeValBtn_clicked Set value button slot function */
void on_setNodeValBtn_clicked();
private:
Ui::CMainWindow *ui;
QStringListModel *m_pModel; // Data model object pointer
};
#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);
// Set the window title
this->setWindowTitle("QListView Simple use ");
//=============== Data model (QStandardItemModel)===============
// Create a string list model object space and specify the parent object
m_pModel = new QStringListModel(ui->listView);
// Set the string list model to the tree view
ui->listView->setModel(m_pModel);
// Add default
m_pModel->setStringList(QStringList()
<< " first line "
<< " The second line "
<< " The third line ");
}
CMainWindow::~CMainWindow()
{
//! Destructor :
//! Some of my friends will find that I don't destruct model object ,
//! That's because I specified the parent object when I got the object space ,
//! When its parent object is destructed , The object whose child object is a pointer will be destructed first .
delete ui;
}
void CMainWindow::on_removeNodeBtn_clicked()
{
// Through the... Of the current position index Get the current line
int row = ui->listView->currentIndex().row();
// If current behavior -1 There is no selection , Then return
if( -1 == row)
{
return;
}
// Remove current row directly
m_pModel->removeRow(row);
}
void CMainWindow::on_addNodeBtn_clicked()
{
// Get the text to be set for the new node
QString str = ui->nodeValEdit->text();
// If the node value is empty, you cannot add a direct return
if(str.isEmpty())
{
return;
}
//! Use insert Insert row
//! ( Because no append function , So get it separately index Set the value )
// Position of the added row
int row = m_pModel->rowCount();
// Insert row
m_pModel->insertRow(row);
// Get the... Of the row just inserted index
QModelIndex index = m_pModel->index(row);
// adopt setData function
m_pModel->setData(index, str);
// Set the newly added row as the current row
ui->listView->setCurrentIndex(index);
}
void CMainWindow::on_getNodeValBtn_clicked()
{
// Get current index
QModelIndex index = ui->listView->currentIndex();
// If current behavior -1 There is no selection , Then return
if( -1 == index.row())
{
return;
}
//! Set the value in the current position to the edit box
//! data The default value of the parameter of the function is Qt::DisplayRole, When an individual obtains the display value, it is not necessary to transfer parameters
ui->nodeValEdit->setText(index.data(Qt::DisplayRole).toString());
}
void CMainWindow::on_setNodeValBtn_clicked()
{
// Get current index
QModelIndex index = ui->listView->currentIndex();
// Get the text to be set by the node
QString str = ui->nodeValEdit->text();
// If current behavior -1 Or if the set string is empty, it returns
if( -1 == index.row() || str.isEmpty())
{
return;
}
// Set the obtained value to the current index position
m_pModel->setData(index, str);
}
summary
QListView At present, there are few in my practical application , So the other two are used View Sudden use QListView I'm not used to it , The main reason is QListView And the other two View The reasons for using different data model objects , That's all I have to ask now .
That's it , Good night …
Related articles
Qt And QTableView Simple use ( Including source code + notes )
Qt And QTreeView Simple use ( Including source code + notes )
Qt The realization of agent ( Button chapter , Including source code + notes )
Qt The realization of agent ( General controls , Including source code + notes )
Qt And QTableView Set the multi column header check box ( Customize QHeaderView)、 Cell check box ( Including source code + notes )
Qt And QSortFilterProxyModel Simple use (QTableView Search function , Including source code + notes )
Friendship tips —— Where can't you understand? It's private , Let's make progress together
( It's not easy to create , Please leave a free praise thank you ^o^/)
notes : This paper summarizes the problems encountered by the author in the process of programming , The content is for reference only , If there are any mistakes, please point out .
notes : If there is any infringement , Please contact the author for deletion
边栏推荐
- Xi'an Jiaotong automation control theory test simulation question [standard answer]
- We strongly recommend more than a dozen necessary plug-ins for idea development
- Detailed explanation of the loss module of mmdet
- Installation and getting started with pytoch
- Xiaosha's lunch
- Rotating frame target detection mmrotate v0.3.1 learning configuration
- Bev instance prediction based on monocular camera (iccv 2021)
- Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (III) [standard answer]
- 剑指 Offer 22. 链表中倒数第k个节点
- Delete the repeating elements in the sorting list (simple questions)
猜你喜欢

Rotation, translation and scaling of unity VR objects

Access is denied encountered when vfpbs calls excel under IIS

Rotating frame target detection mmrotate v0.3.1 training dota data set (II)

Sound network, standing in the "soil" of the Internet of things

Intellj idea generates jar packages for projects containing external lib to other projects. The method refers to the jar package written by itself

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

旋转框目标检测mmrotate v0.3.1 训练DOTA数据集(二)

Delete the repeating elements in the sorting list (simple questions)

Bev instance prediction based on monocular camera (iccv 2021)
![[typescript] cannot redeclare block range variables](/img/52/2fd3071ca9e3c5023c6b65961e2cf7.jpg)
[typescript] cannot redeclare block range variables
随机推荐
Responding with flow layout
token 过期后,如何自动续期?
Revit secondary development - use panel function without opening the project
Revit二次開發---未打開項目使用面板功能
使用码云PublicHoliday项目判断某天是否为工作日
Database SQL language 04 subquery and grouping function
Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (II)
Unity screenshot method
Summary of common loss functions in pytorch
网络变压器怎么判断好坏?网络滤波变压器坏了一般是什么症状?
[notes] unity Scrollview button page turning
You don't know how to deduce the location where HashSet stores elements?
Who is promoting the new inflection point of audio and video industry in 2022?
unity 扫描圈 圆扩展方法
Unity 3D model operation and UI conflict Scrollview
Redistemplate common method summary
领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
Word frequency statistics (string, list)
Super comprehensive summary | related improvement codes of orb-slam2 / orb-slam3!
Online assignment of C language program design in the 22nd spring of Western Polytechnic University