当前位置:网站首页>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 .
边栏推荐
- 2.1 use of variables
- February 14-20, 2022 (osgear source code debugging +ue4 video +ogremain source code transcription)
- Explore the internal mechanism of modern browsers (I) (original translation)
- How can the outside world get values when using nodejs to link MySQL
- Rd file name conflict when extending a S4 method of some other package
- 2.4 conversion of different data types
- Change deepin to Alibaba image source
- Fingerprint password lock based on Hal Library
- 1.4 learn more about functions
- 1.5 learn to find mistakes first
猜你喜欢
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
2.6 formula calculation
Make a simple text logo with DW
An old programmer gave it to college students
2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
Oak-d raspberry pie cloud project [with detailed code]
2.4 conversion of different data types
Xctf attack and defense world crypto master advanced area olddriver
Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
随机推荐
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
Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
FPGA 学习笔记:Vivado 2019.1 工程创建
【c】 Digital bomb
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
Test changes in Devops mode -- learning and thinking
Part 27 supplement (27) buttons of QML basic elements
Global and Chinese market of cyanuric acid 2022-2028: Research Report on technology, participants, trends, market size and share
Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
Unittest framework is basically used
Ruby replaces gem Alibaba image
Change deepin to Alibaba image source
Wargames study notes -- Leviathan
FAQs for datawhale learning!
Machine learning support vector machine SVM
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
P5.js development - setting
6006. Take out the minimum number of magic beans
2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
6. Data agent object Defineproperty method