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

 Insert picture description here

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