当前位置:网站首页>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 .

边栏推荐
- Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue
- Oak-d raspberry pie cloud project [with detailed code]
- Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
- Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
- AcWing 1460. Where am i?
- BOC protected amino acid porphyrins TAPP ala BOC, TAPP Phe BOC, TAPP Trp BOC, Zn · TAPP ala BOC, Zn · TAPP Phe BOC, Zn · TAPP Trp BOC Qiyue
- 4. Data splitting of Flink real-time project
- Print linked list from end to end
- Machine learning support vector machine SVM
- Global and Chinese market of micro positioning technology 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

2022 Xinjiang latest road transportation safety officer simulation examination questions and answers

BOC protected tryptophan zinc porphyrin (Zn · TAPP Trp BOC) / copper porphyrin (Cu · TAPP Trp BOC) / cobalt porphyrin (cobalt · TAPP Trp BOC) / iron porphyrin (Fe · TAPP Trp BOC) / Qiyue supply

Wargames study notes -- Leviathan

PR 2021 quick start tutorial, material import and management

JMeter connection database

Change deepin to Alibaba image source

JMeter plug-in installation

44. Concurrent programming theory

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

CMD implements the language conversion of locale non Unicode programs
随机推荐
2.4 conversion of different data types
App compliance
How to read the source code [debug and observe the source code]
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
2.2 integer
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
44. Concurrent programming theory
Commands related to files and directories
Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]
Wargames study notes -- Leviathan
About callback function and hook function
Rd file name conflict when extending a S4 method of some other package
Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
Global and Chinese markets for medical temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
2022-06-28 网工进阶(十三)IS-IS-路由过滤、路由汇总、认证、影响ISIS邻居关系建立的因素、其他命令和特性
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
Xctf attack and defense world crypto master advanced area olddriver
Fingerprint password lock based on Hal Library
2.7 format output of values