我对QTableWidget + QComboBox的组合很感兴趣。
如屏幕截图所示,我想要的是,每当我想在ADD按钮之后在表中插入新项时,新行应该插入到表中,在结束时,我希望下拉框中包含文本“允许和阻止.”。
webFilterCat = new QTableWidget(0);
webFilterCat -> verticalHeader()->setDefaultSectionSize(15);
webFilterCat -> setMaximumWidth(310);
webFilterCat -> setMinimumWidth(310);
webFilterCat -> setMaximumHeight(170);
webFilterCat -> setMinimumHeight(170);
/* Set Row and Column Count*/
//webFilterCat->setRowCount(10);
webFilterCat->setColumnCount(3);
/* Table Header */
tableHeader<<"Category"<<"Status"<<"Action";
webFilterCat -> setHorizontalHeaderLabels(tableHeader);
webFilterCat -> horizontalHeader()->setDefaultSectionSize(100);
// ------ Insert Data -------
webFilterCat->insertRow ( webFilterCat->rowCount() );
webFilterCat->setItem(( webFilterCat->rowCount() -1 ), 0, new QTableWidgetItem(extName));
webFilterCat -> setItem(( webFilterCat->rowCount() -1 ), 1, new QTableWidgetItem("Block"));
webFilterCat -> setCellWidget ( ( webFilterCat->rowCount() -1 ), 2, new QComboBox( webFilterCat ) );
在将项添加到表中后,我将在每行获取QComboBox,但我不能将标签(即允许或阻止)设置为QComboBox,以及如何在QComboBox中更改值之后访问该perticular行。
我使用qt4.2,使用linux终端编译代码。
简而言之,不使用qt创建者,而是使用命令行qt.。
发布于 2020-05-16 13:42:55
我找到了解决问题的办法。
webFilterCat = new QTableWidget(0);
webFilterCat -> verticalHeader()->setDefaultSectionSize(15);
webFilterCat -> setMaximumWidth(310);
webFilterCat -> setMinimumWidth(310);
webFilterCat -> setMaximumHeight(170);
webFilterCat -> setMinimumHeight(170);
/* Set Row and Column Count*/
//webFilterCat->setRowCount(10);
webFilterCat->setColumnCount(3);
/* Table Header */
tableHeader<<"Category"<<"Status"<<"Action";
webFilterCat -> setHorizontalHeaderLabels(tableHeader);
webFilterCat -> horizontalHeader()->setDefaultSectionSize(100);
for(int i=0; i<row_count; i++){
// ------ Insert Data -------
webFilterCat->insertRow ( webFilterCat->rowCount() );
webFilterCat->setItem(( webFilterCat->rowCount() -1 ), 0, new QTableWidgetItem(extName));
webFilterCat -> setItem(( webFilterCat->rowCount() -1 ), 1, new QTableWidgetItem("Block"));
/*webFilterCat -> setCellWidget ( ( webFilterCat->rowCount() -1 ), 2, new QComboBox( webFilterCat ) );*/
cmbCatAllowOrBlock = new QComboBox ( 0 );
cmbCatAllowOrBlock -> setFixedSize ( 80, 20 ); // 75, 20
cmbCatAllowOrBlock -> addItem ( "Allow" );
cmbCatAllowOrBlock -> addItem ( "Block" );
cmbCatAllowOrBlock -> setCurrentIndex( 0 );
webFilterCat -> setCellWidget ( ( webFilterCat->rowCount() -1 ), 2, cmbCatAllowOrBlock );
}
我得到了我需要的解决方案,如下所示。
并得到了这个问题的解决方案。
在QComboBox中值改变后如何访问那个置换行?
我所做的,在读QComboBox的时候。我不会试着去读QComboBox。我只需要更改QComboBox的状态,所以我将读取整个表并与前一个表进行比较,并能够找到更改。
因此,如何逐行读取QComboBox是
for(int i=0;i<(webFilterCat->rowCount() -1);i++){
QComboBox* combo=(QComboBox*) webFilterCat -> cellWidget(i, 2);
qDebug() << combo -> currentText();
}
https://stackoverflow.com/questions/61797260
复制相似问题