当前位置:网站首页>Usage Summary of item views and item widgets controls in QT

Usage Summary of item views and item widgets controls in QT

2022-06-23 06:43:00 Crazy excavator


0、 Preface

In the use of QT When making data table presentation , Most of them use tables , List or type tree structure ,QT The commonly used controls in show item view(List 、Tree、Table、Colum、undo view) also item wigets(List 、Tree、Table、Colum、undo view) How to choose , It mainly looks at the difference and connection between the two :

0.1 contact

 Insert picture description here
In terms of inheritance , item wigets Are inherited from the corresponding item wiew, Generally speaking, in terms of function items widget Easier to use .
 Insert picture description here

0.2 difference

1、 item wigets It's easier to use , and item wiew The usage of is relatively complicated .
2、item views It can store a lot of data ( For example, hundreds of thousands or even millions ), Users will not get stuck when browsing the data in the table ; Even though item wigets It can also be used to store large amounts of data , However, users may get stuck when using , And the more data displayed , The more obvious the similar phenomenon is .
3、 They are different when loading data sources :item views Pass count attempt / Modeling separates data from presentation , Therefore, data binding is required when using , Colleagues modify data directly in model modify ,item wigets The data source can be considered to be loaded normally through the function , It is more convenient to use .

The following is a specific example ( item wigets choose QTableWidget , item wiew choose QTableView )

4、 Other view The main function :

QListView: Used to display single column list data , It is applicable to the operation of one-dimensional data
QTreeView: Used to display tree structure data , It is applicable to the operation of tree structure data
QTableView: Used to display tabular data , It is applicable to the operation of two-dimensional tabular data
QColumnView: Use multiple QListView Displays the tree hierarchy , One layer of the tree structure uses a QListView Show
QHeaderView: A view component that provides row headers or list headers , Such as QTableView Row header and list header
UI There are two types on the control panel , Of course, you can also directly define .


1、item views( With QTableView As an example )

view The simple code is as follows :( Omit the inclusion of some header files )

Widget::Widget(QWidget *parent): QTableView(parent)
{
    
   QStandardItemModel* model = new QStandardItemModel(this);
   model->setItem(0, 0, new QStandardItem("test1")); 
   model->setItem(0, 1, new QStandardItem("test2"));
   model->setItem(0, 2, new QStandardItem("test3"));
   this->setModel(model);
}

It's simple , Direct one view Put one in model, All data changes are in model function , The interface is displayed in view On , Although there is no agent here , But the default proxy is already used :QStyledItemDelegate;

2、item widgets( With QTableWidget As an example )

QTableWidget The simple code is as follows :( Omit the inclusion of some header files )

    // Set the header 
    TableWidget.setHorizontalHeaderLabels(QStringList() << "1" << "2" << "3");
    // Set the contents of each row in the table 
    TableWidget.setItem(0,0,new QTableWidgetItem("test1"));
    TableWidget.setItem(0,1,new QTableWidgetItem("test1"));

Friendly to novices , Simple setup .

3、 Customize

 Insert picture description here
I understand View - Model - The relationship between agents : Trying is actually part of the presentation , such as ui Show part , A model is actually a collection of data , Agents actually define a way of trying to present data , All three have default types .
Just say View/model situation , When you need to re edit model perhaps delegeta Well
1、 Using standard model, Such as QStandardiTemModel, When providing data , It may not reach the optimization we want , Such as real-time data , Then we can inherit again QStandardiTemModel perhaps QAbstractItemModel Re actualize data function
2、 Redefining delegate When , Mainly for customized things , If there is a drop-down box in the table , Check the box for such content , By default, the requirements cannot be met , You need to customize delegate.
 Insert picture description here
The main suggestions here are inherited from QStyledItemDelegate( I am too lazy to type )
 Insert picture description here


summary

1、QTreeView Customize model: Large data volume display – Performance and memory optimization
2、QT Official documents

原网站

版权声明
本文为[Crazy excavator]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230520368749.html