当前位置:网站首页>QT tablewidget insert qcombobox drop-down box
QT tablewidget insert qcombobox drop-down box
2022-04-23 18:15:00 【Talent、me】
Don't talk much , Here are the renderings , Go straight to the code
initialization QTableWidget
/************************************************* Function: partTableInit() Description: initialization tablewidget, And set header column text Input: QTableWidget * tableObject -> List control Output: Return: *************************************************/
void Widget::partTableInit(QTableWidget * tableObject)
{
tableObject->clear();
QStringList tableHeaderList;
tableHeaderList<<tr(" Student number ")<<tr(" full name ")<<tr(" class ")<<tr(" identity ");
tableObject->setRowCount(14);
tableObject->setColumnCount(tableHeaderList.size()); // Header column text settings
for (int i=0;i<tableHeaderList.size();i++) {
tableObject->setHorizontalHeaderItem(i,new QTableWidgetItem(tableHeaderList.at(i)));
tableObject->setColumnWidth(i,ui->tableWidget->width()/tableHeaderList.size());
}
tableObject->horizontalHeader()->setStretchLastSection(true);
tableObject->verticalHeader()->setMinimumSectionSize(35);
tableObject->resizeRowsToContents();
tableObject->setSelectionBehavior(QAbstractItemView::SelectRows);// Line selection
tableObject->setAlternatingRowColors(true);
tableObject->setFocusPolicy(Qt::NoFocus); // Remove the selected dashed box
tableObject->verticalHeader()->setVisible(false); // Set the vertical head invisible
tableObject->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);// Remove the horizontal scroll bar
tableObject->setEditTriggers(QAbstractItemView::NoEditTriggers);// Can't write
tableObject->setSelectionMode(QAbstractItemView::SingleSelection);// You can only select... Once
}
insert data
/************************************************* Function: sqlLoadData() Description: from sqlite Get content , Parse update to tablewidget in Input:QTableWidget *tableObject ->list object Output: Return: *************************************************/
void Widget::loadData(QTableWidget *tableObject)
{
tableObject->setItem(0,0, new QTableWidgetItem(2021));
tableObject->item(0,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
tableObject->setItem(0,1, new QTableWidgetItem( Xiao Ming ));
tableObject->item(0,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
QComboBox *classBox = new QComboBox;
classBox->addItem(" In grade one ");
classBox->addItem(" second grade ");
classBox->addItem(" Third grade ");
classBox->addItem(" Fourth grade ");
classBox->addItem(" Fifth grade ");
tableObject->setCellWidget(0,2,classBox);
classBox->setCurrentText(" In grade one ");
tableObject->setItem(0,3, new QTableWidgetItem(" Student "));
tableObject->item(0,3)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
tableObject->resizeRowsToContents();
}
How to get the data in the table
1. The first one is : Get... Directly from coordinates
/* How to get ordinary table controls */
QString strValue = tableObject->item(0,1)->text();
/* The drop-down box in the table gets the selected value */
QString strValue=dynamic_cast<QComboBox *>(ui->tableWidget->cellWidget(0,2))->currentText();
2. The second kind : Click on QTableWidget The table signal in the triggers
The signal :doubleClicked(const QModelIndex &)
itemDoubleClicked(QTableWidgetItem *)
Slot : void itemDoubleClick(QTableWidgetItem *item)
{
int row=item->row();
QString tableObject = item->tableWidget()->objectName();
QString itemValue = item->tableWidget()->item(row,1)->text().trimmed();
QString itemValue = dynamic_cast<QComboBox *>(item->tableWidget()->cellWidget(0,2))->currentText();// How to get the drop-down box in the table
}
The above code actually uses the database Sqlite To create a student list , And then it shows up in QTableWidget Control to modify student information , The above code block is a simple insertion, and the information is displayed on the list .
版权声明
本文为[Talent、me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210610471795.html
边栏推荐
- C language input and output (printf and scanf functions, putchar and getchar functions)
- Format problems encountered in word typesetting
- Classes and objects
- 【ACM】376. Swing sequence
- Log4j2 cross thread print traceid
- Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
- STM32 learning record 0008 - GPIO things 1
- Yolov4 pruning [with code]
- 消费者灰度实现思路
- Pointers in rust: box, RC, cell, refcell
猜你喜欢
Resolve the error Max virtual memory areas VM max_ map_ count [65530] is too low, increase to at least [262144]
[UDS unified diagnostic service] v. diagnostic application example: Flash bootloader
【ACM】509. 斐波那契数(dp五部曲)
Cygwin64 right click to add menu, and open cygwin64 here
Gobang game based on pyGame Library
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
深度学习经典网络解析目标检测篇(一):R-CNN
Install pyshp Library
Random number generation of C #
【ACM】455. Distribute Biscuits (1. Give priority to big biscuits to big appetite; 2. Traverse two arrays with only one for loop (use subscript index -- to traverse another array))
随机推荐
Yolov4 pruning [with code]
函数递归以及趣味问题的解决
线上怎么确定期货账户安全的?
读取excel,int 数字时间转时间
From introduction to mastery of MATLAB (2)
C [file operation] read TXT text by line
Docker installation MySQL
Implement a simple function to calculate the sum of all integers between M ~ n (m < n)
C network related operations
Crawler for querying nicknames and avatars based on qqwebapi
Calculation of fishing net road density
Robocode tutorial 7 - Radar locking
Random number generation of C #
Matlab tips (6) comparison of seven filtering methods
The difference between deep copy and shallow copy
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
Qt读写XML文件(含源码+注释)
Log4j2 cross thread print traceid
Dock installation redis
mysql自动启动设置用Systemctl start mysqld启动