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

边栏推荐
- Initialization and instantiation
- Wargames study notes -- Leviathan
- Micro service knowledge sorting - cache technology
- 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
- Change deepin to Alibaba image source
- Realize user registration and login
- Find a line in a file and remove it
- Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
- Leetcode daily question solution: 540 A single element in an ordered array
- 原生表格-滚动-合并功能
猜你喜欢

2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】

2.7 format output of values

Test changes in Devops mode -- learning and thinking

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

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

Camera calibration (I): robot hand eye calibration
![AI enhanced safety monitoring project [with detailed code]](/img/a9/cb93f349229e86cbb05ad196ae9553.jpg)
AI enhanced safety monitoring project [with detailed code]

Point cloud data denoising

5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes

BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
随机推荐
Microservice knowledge sorting - search technology and automatic deployment technology
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
AST (Abstract Syntax Tree)
Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
Phpstudy set LAN access
Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation
2022-06-28 网工进阶(十三)IS-IS-路由过滤、路由汇总、认证、影响ISIS邻居关系建立的因素、其他命令和特性
Derivation of decision tree theory
How to check the permission to write to a directory or file- How do you check for permissions to write to a directory or file?
Change deepin to Alibaba image source
CMD implements the language conversion of locale non Unicode programs
P5.js development - setting
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
PR 2021 quick start tutorial, material import and management
Upgrade PIP and install Libraries
Day6 merge two ordered arrays
6006. Take out the minimum number of magic beans
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
Day10 -- forced login, token refresh and JWT disable
1.5 learn to find mistakes first