当前位置:网站首页>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 .
边栏推荐
- 2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
- 2.7 format output of values
- Test access criteria
- Assign the CMD command execution result to a variable
- Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
- Native table - scroll - merge function
- Parental delegation mechanism
- Plan for the first half of 2022 -- pass the PMP Exam
- Test changes in Devops mode -- learning and thinking
- [effective Objective-C] - block and grand central distribution
猜你喜欢
Wechat applet quick start (including NPM package use and mobx status management)
2.2 integer
2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration
Explore the internal mechanism of modern browsers (I) (original translation)
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
Machine learning support vector machine SVM
Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
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
Professional interpretation | how to become an SQL developer
[effective Objective-C] - block and grand central distribution
随机推荐
Micro service knowledge sorting - asynchronous communication technology
Unittest framework is basically used
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
Test changes in Devops mode -- learning and thinking
MySQL learning notes - single table query
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
Global and Chinese markets of polyimide tubes for electronics 2022-2028: Research Report on technology, participants, trends, market size and share
2.4 conversion of different data types
Promethus
Part 27 supplement (27) buttons of QML basic elements
NFT without IPFs and completely on the chain?
47. Process lock & process pool & Collaboration
Global and Chinese market of charity software 2022-2028: Research Report on technology, participants, trends, market size and share
Microservice framework - frequently asked questions
Plan for the first half of 2022 -- pass the PMP Exam
Class loading process
原生表格-滚动-合并功能
Micro service knowledge sorting - cache technology
The simplicity of laravel
Acquisition and transmission of parameters in automatic testing of JMeter interface