当前位置:网站首页>Qtablewidget control of QT
Qtablewidget control of QT
2022-07-03 20:11:00 【Little ash of the prairie】
QTableWidget yes QT Table component classes in . It is generally used to display data with multiple rows and columns , yes QT One of the controls used more in .
1、QTableWidgetItem object
QTableWidget Every cell in is a QTableWidgetItem object , So let's introduce it first QTableWidgetItem The common method of .
1.1、 Set text content
void QTableWidgetItem::setText(const QString &text)
1.2、 Set alignment
void QTableWidgetItem::setTextAlignment(int alignment)
1.3、 Set cell background color
void QTableWidgetItem::setBackground(const QBrush &brush)
1.4、 Set the cell display icon
void QTableWidgetItem::setIcon(const QIcon &icon)
1.5、 Set cell font
void QTableWidgetItem::setFont(const QFont &font)
1.6、 Get cell text content
QString QTableWidgetItem::text() const
2、 Set up QTableWidget The header
// Set number of columns
ui->tableWidget->setColumnCount(3);
QTableWidgetItem *item0 = new QTableWidgetItem;
QTableWidgetItem *item1 = new QTableWidgetItem;
QTableWidgetItem *item2 = new QTableWidgetItem;
// Set display content
item0->setText(" full name ");
item1->setText(" Age ");
item2->setText(" Student number ");
// Set the horizontal header
ui->tableWidget->setHorizontalHeaderItem(0, item0);
ui->tableWidget->setHorizontalHeaderItem(1, item1);
ui->tableWidget->setHorizontalHeaderItem(2, item2);effect

3、 Initialization data
// Initialization data
// When adding data , Be sure to set the number of rows , Otherwise, the data will not be displayed
ui->tableWidget->setRowCount(2);
QTableWidgetItem* name0 = new QTableWidgetItem;
QTableWidgetItem* name1 = new QTableWidgetItem;
QTableWidgetItem* age0 = new QTableWidgetItem;
QTableWidgetItem* age1 = new QTableWidgetItem;
QTableWidgetItem* code0 = new QTableWidgetItem;
QTableWidgetItem* code1 = new QTableWidgetItem;
name0->setText(" Zhang San ");
name1->setText(" Li Si ");
age0->setText("28");
age1->setText("26");
code0->setText("1000");
code1->setText("1001");
// Set... For a specified row or column item
ui->tableWidget->setItem(0, 0, name0);
ui->tableWidget->setItem(1, 0, name1);
ui->tableWidget->setItem(0, 1, age0);
ui->tableWidget->setItem(1, 1, age1);
ui->tableWidget->setItem(0, 2, code0);
ui->tableWidget->setItem(1, 2, code1);effect

4、 insert data
// Insert data at the end
int curRow = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(curRow);
QTableWidgetItem* name2 = new QTableWidgetItem;
QTableWidgetItem* age2 = new QTableWidgetItem;
QTableWidgetItem* code2 = new QTableWidgetItem;
name2->setText(" Wang Wu ");
age2->setText("19");
code2->setText("1002");
ui->tableWidget->setItem(curRow, 0, name2);
ui->tableWidget->setItem(curRow, 1, age2);
ui->tableWidget->setItem(curRow, 2, code2);effect

5、 Traversal data
// Traversal data
QTableWidgetItem* item;
QString str;
for(int i = 0; i < ui->tableWidget->rowCount(); i++){
for(int j = 0; j < ui->tableWidget->columnCount(); j++){
item = ui->tableWidget->item(i, j);
str = str + item->text() + " ";
}
}
qDebug()<<"str = "<<str;effect
![]()
6、 Common signals
When cells switch , Send two signals
//currentRow, currentColumn The currently clicked cell
//previousRow, previousColumn Cells that previously had focus
void QTableWidget::currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
//current The currently clicked cell
//previous Cells that previously had focus
void QTableWidget::currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)7、 Delete the selected line
int curRow = ui->tableWidget->currentRow();
ui->tableWidget->removeRow(curRow);8、 Setting of header
8.1、 Set the last cell of each row without spaces
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);Look directly at the effect

8.2、 Set adaptive size
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);effect

8.3、 Set equal width
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);effect
9、 Other common settings
9.1、 Gets the number of rows and columns
int QTableWidget::rowCount() const
int QTableWidget::columnCount() const
9.2、 Get the row number and column number of the current cell
int QTableWidget::currentRow() const
int QTableWidget::currentColumn() const
9.3、 Set whether the cell content can be edited
void setEditTriggers(EditTriggers triggers)
9.4、 Set whether the selection method is cell selection or row selection
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
QAbstractItemView::SelectItems // Cell selection mode
QAbstractItemView::SelectRows // Row selection mode
QAbstractItemView::SelectColumns // Column selection mode
Qt The use of the manual
The above description is QTableWidget Some commonly used methods , Using these methods for basic data display and reading is completely enough , If you want to use some other functions , You can check it by yourself qt manual .
install qt after , We enter Assistant, Then open the .

Here we enter the name of the control we want to query , All the methods provided by this control will be queried on the right .

边栏推荐
- 1.4 learn more about functions
- 2022-06-25 advanced network engineering (XI) IS-IS synchronization process of three tables (neighbor table, routing table, link state database table), LSP, cSNP, psnp, LSP
- Unittest framework is basically used
- Exercises of function recursion
- Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
- Change deepin to Alibaba image source
- Point cloud data denoising
- Popularize the basics of IP routing
- HCIA-USG Security Policy
- 2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
猜你喜欢

Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system

How to improve data security by renting servers in Hong Kong

10 smart contract developer tools that miss and lose

2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)

2.3 other data types

Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue

Phpstudy set LAN access

BOC protected alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC supplied by Qiyu

Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro

Point cloud data denoising
随机推荐
4. Data splitting of Flink real-time project
原生表格-滚动-合并功能
First knowledge of database
unittest框架基本使用
Chapter 1: find all factorial sums, Grand Prix site unified programming, three factorial sums, graphic point scanning, recursive factorial n of n!, Find the factorial n of n!, King Shehan miscalculate
Phpstudy set LAN access
Pat grade B 1009 is ironic (20 points)
Leetcode daily question solution: 540 A single element in an ordered array
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
An old programmer gave it to college students
Upgrade PIP and install Libraries
Use of aggregate functions
【leetcode】1027. Longest arithmetic sequence (dynamic programming)
Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
Implementation of stack
Global and Chinese markets of polyimide tubes for electronics 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of rubidium standard 2022-2028: Research Report on technology, participants, trends, market size and share
4. Data binding
BOC protected alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC supplied by Qiyu
HCIA-USG Security Policy