如何在Qtableview中获取所选组合框的行值

How to get rowvalue of selected combobox in Qtableview?

本文关键字:组合 Qtableview 获取      更新时间:2024-09-28

我有Qtableviews,我在table2中添加了combobox,combobox中的值从table1 col0中获取,所以当用户在combo2中选择项目时,我需要获取所选项目的行值?

  1. 如何使用indexwidget?

  2. 在得到行值之后,我可以比较组合框文本并执行计算。由于我共享的是有效的,但当用户在2行(diff(组合框中选择相同的值时,它就不起作用了(2次内无法获取数据(。

void Gear_model::calR2() {
QAbstractItemModel* table1 = ui.tableView->model();
QAbstractItemModel* table2 = ui.tableView_2->model();
QComboBox* combo2;
QString col2;
for (int irowsl = 0, maxI = table2->rowCount(); irowsl < maxI; ++irowsl)
{
const QModelIndex idx1 = table2->index(irowsl, 2);
combo2 = qobject_cast<QComboBox*>(ui.tableView_2->indexWidget(idx1)); 
col2 = combo2->currentText();
for (int irowsM = 0, maxI = table2->rowCount(); irowsM < maxI; ++irowsM) {
qDebug() << "combo2 =" << col2;                                        
if (col2 == table1->data(table1->index(irowsM, 0)).toString()) {
double pitchradius2 = table1->data(table1->index(irowsM, 6)).toDouble();
table2->setData(table2->index(irowsl, 10), radius2);
qDebug() << "R2 =" << col2 << radius2;
}
}
}

无法弄清楚如何为每一行获取组合框的选定值?

我找到了解决方案燃烧后2我添加了

if (combo2!=NULL) {
//my previous code
}