我被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中的值被改变后访问该行。
我使用的是 qt 4.2 和使用 linux终端.
简而言之就是不使用qt creator,使用命令行qt。
我找到了我的问题的解决方案。
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中的值被改变后访问那条perticular行?
我做了什么,在读取每行的QComboBox时,我不会尝试读取每行的QComboBox。我不会尝试读取每行的QComboBox。我只需要QComboBox的变化状态,所以我将读取整个表格,并与之前的表格进行比较,从而找到变化。
那么,如何逐行读取QComboBox呢?
for(int i=0;i<(webFilterCat->rowCount() -1);i++){
QComboBox* combo=(QComboBox*) webFilterCat -> cellWidget(i, 2);
qDebug() << combo -> currentText();
}