Qt TableWidget插入QComboBox下拉框

  • Post author:
  • Post category:其他



话不多说,以下是效果图,直接上代码

在这里插入图片描述



初始化QTableWidget

/*************************************************
Function: partTableInit()
Description: 初始化tablewidget,并设置表头列文字
Input:  QTableWidget * tableObject   ->列表控件
Output:
Return:
*************************************************/
void Widget::partTableInit(QTableWidget * tableObject)
{
    tableObject->clear();
    QStringList tableHeaderList;
    tableHeaderList<<tr("学号")<<tr("姓名")<<tr("班级")<<tr("身份");
    tableObject->setRowCount(14); 
    tableObject->setColumnCount(tableHeaderList.size());          //表头列文本设置
   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);//行选中
   tableObject->setAlternatingRowColors(true);
   tableObject->setFocusPolicy(Qt::NoFocus); //去除选中虚线框
   tableObject->verticalHeader()->setVisible(false); //设置垂直头不可见
   tableObject->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//去掉水平滚动条
   tableObject->setEditTriggers(QAbstractItemView::NoEditTriggers);//不能编写
   tableObject->setSelectionMode(QAbstractItemView::SingleSelection);//只能单次选中
}



插入数据

/*************************************************
Function: sqlLoadData()
Description: 从sqlite获取内容,解析更新到tablewidget中
Input:QTableWidget *tableObject ->list对象
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(小明));
        tableObject->item(0,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        QComboBox *classBox = new QComboBox;
        classBox->addItem("一年级");
        classBox->addItem("二年级");
        classBox->addItem("三年级");
        classBox->addItem("四年级");
        classBox->addItem("五年级");
        tableObject->setCellWidget(0,2,classBox);
        classBox->setCurrentText("一年级");
      	tableObject->setItem(0,3, new QTableWidgetItem("学生"));
        tableObject->item(0,3)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    	tableObject->resizeRowsToContents();

}



如何获取表格里的数据

1.第一种:直接通过坐标去获取
/*普通表格控件获取方式*/
QString strValue = tableObject->item(0,1)->text();
/*表格中下拉框获取选中值*/
QString strValue=dynamic_cast<QComboBox *>(ui->tableWidget->cellWidget(0,2))->currentText();

2.第二种:点击QTableWidget里的表格信号触发
信号:doubleClicked(const QModelIndex &)
	 itemDoubleClicked(QTableWidgetItem *): 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();//表格里的下拉框获取方式
     }
	 


上述代码其实用了数据库Sqlite去创建一个学生表,然后显示在QTableWidget控件上去修改学生信息,上述代码块是单纯的插入信息显示在列表上。



版权声明:本文为qq_45147279原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。