当前位置:网站首页>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
边栏推荐
- MATLAB从入门到精通(二)
- Quantexa CDI(场景决策智能)Syneo平台介绍
- Robocode tutorial 5 - enemy class
- ArcGIS table to excel exceeds the upper limit, conversion failed
- Win1远程出现“这可能是由于credssp加密oracle修正”解决办法
- 登录和发布文章功能测试
- Operators in C language
- [UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
- Docker 安裝 Redis
- Using files to save data (C language)
猜你喜欢
Installation du docker redis
re正则表达式
硬核解析Promise對象(這七個必會的常用API和七個關鍵問題你都了解嗎?)
Robocode tutorial 5 - enemy class
How to install jsonpath package
C network related operations
MySQL_ 01_ Simple data retrieval
Notepad + + replaces tabs with spaces
The vivado project corresponding to the board is generated by TCL script
硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
随机推荐
The vivado project corresponding to the board is generated by TCL script
Queue solving Joseph problem
Calculation of fishing net road density
C byte array (byte []) and string are converted to each other
C [file operation] read TXT text by line
Multifunctional toolbox wechat applet source code
GDAL + ogr learning
Identification verification code
【ACM】455. 分发饼干(1. 大饼干优先喂给大胃口;2. 遍历两个数组可以只用一个for循环(用下标索引--来遍历另一个数组))
Secure credit
Format problems encountered in word typesetting
Crawl the product data of Xiaomi Youpin app
Robocode Tutorial 4 - robocode's game physics
Implement a simple function to calculate the sum of all integers between M ~ n (m < n)
消费者灰度实现思路
Matlab tips (6) comparison of seven filtering methods
PowerDesigner various font settings; Preview font setting; SQL font settings
WIN1 remote "this may be due to credssp encryption Oracle correction" solution
ArcGIS license error -15 solution
线上怎么确定期货账户安全的?