【问题标题】:How to get QTableWidget + QComboBox together and how to access perticular row if value is changed from QComboBox of same row如何将 QTableWidget + QComboBox 放在一起,如果值从同一行的 QComboBox 更改,如何访问特定行
【发布时间】:2023-03-07 02:51:01
【问题描述】:

我被 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。

【问题讨论】:

  • 您可能会在行组合框指针和相应的行号之间存储某种映射,例如:std::map&lt;QComboBox *, int&gt;。激活组合框项后,您可以知道它用于哪一行。
  • @vahancho 没有。不幸的是我不能使用。因为有时用户可以删除一行,这样逻辑就会在这里失败。先建议如何在QComboBox中插入标签
  • QDataWidgetMapper 不是您要找的吗?
  • 你好@MasterAler我是QT的新手。我不知道 QDataWidgetMapper。我只想通过钩子或厨师来做我的口味。在将项目添加到表中后,我在每一行都得到 QComboBox 但我无法将标签(即允许或阻止)设置为 QComboBox 以及如何在 QComboBox 中更改值后访问该特定行。告诉我该怎么做。

标签: qt qt5 qtablewidget qcombobox qtwidgets


【解决方案1】:

我找到了解决问题的方法。

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();
}

【讨论】:

    猜你喜欢
    • 2021-10-04
    • 2018-02-15
    • 2016-11-08
    • 2016-08-19
    • 1970-01-01
    • 2022-01-21
    • 2017-02-04
    • 2023-04-07
    相关资源
    最近更新 更多