【问题标题】:Qcombobox remove and add item in C++ QtQcombobox在C++ Qt中删除和添加项目
【发布时间】:2021-05-08 08:53:46
【问题描述】:

在 Qtableview2 第 1 列中创建组合框并从 Qtableview1 第 1 列传递值 所以我将 column1 table1 值存储在 Qstringlist 中并传递给组合框

void cymodel::rowvalues() {
    QAbstractItemModel* table1 = ui.tableView->model();
    QAbstractItemModel* table2 = ui.tableView_2->model();
    QStringList colvallist1;

    for (int r = 0, maxI = table1->rowCount(); r < maxI; ++r)
        colvallist1.append(table1->data(table1->index(r, 0)).toString());//store value in stringlist
        for (int i = 0, maxI = table2->rowCount(); i < maxI; ++i)//for all rows
        {
            const QModelIndex idx = table2->index(i, 1);
            QComboBox* combo = qobject_cast<QComboBox*>(ui.tableView_2->indexWidget(idx));
            if (!combo)
            {
                combo = new QComboBox(); // make combo  
                ui.tableView_2->setIndexWidget(idx, combo);// add combo
            }
        //    combo->model()->removeRows(0, combo->count(), combo->rootModelIndex());
            colvallist1.removeDuplicates(); // clear duplicates in colvallist1
            colvallist1.removeAll(QString("")); //remove empty row data
            combo->setPlaceholderText(QString(" "));
            combo->addItems(colvallist1);
}
}

connect(ui.tableView->model(), &QAbstractItemModel::dataChanged, this,
              &cymodel::rowvalues);

使用 removerows() 每次都会删除所有项目,如果我在 column0 table1 中添加了一些值,然后在组合框中选择并再次在 table1 Column0 中添加值,那时组合框选择消失 但是如果我不使用 removerows() 那么当我向组合框添加新项目时,它会添加多次,比如添加 2 个值,然后我在 table1 col 中再添加 2 个,然后在组合框中它变成 4

那么,如何添加出现在 colvallist1 中但尚未出现在组合中的那些并删除那些未出现在 colvallist1 中的那些??

谢谢大家帮忙!!

【问题讨论】:

  • 有什么建议吗??
  • 我在单独的循环中添加项目,这个删除已经存在的项目的代码,但不能正常工作?如果项目存在于猜测列表中,那么它也被删除了???组合框中仅显示一项 QStringList itemsInComboBox; for (int item = 0; item count(); ++item) { //检查组合框 itemsInComboBox itemText(item); for (int index = 0; index removeItem(item); } }
  • 这个问题很难吗??

标签: c++ qt qcombobox


【解决方案1】:

thnks,,,我解决了这个.. 删除组合框中不在 colvallist1.. 中的项目而不更改选择 这是代码--

void cymodel::rowvalues() {
    QAbstractItemModel* table1 = ui.tableView->model();
    QAbstractItemModel* table2 = ui.tableView_2->model();
    QStringList colvallist1;

    for (int r = 0, maxI = table1->rowCount(); r < maxI; ++r)
        colvallist1.append(table1->data(table1->index(r, 0)).toString());//store value in stringlist
        for (int i = 0, maxI = table2->rowCount(); i < maxI; ++i)//for all rows
        {
            const QModelIndex idx = table2->index(i, 1);
            QComboBox* combo1 = qobject_cast<QComboBox*>(ui.tableView_2- 
             >indexWidget(idx));
        if (!combo1)
        {
            combo1 = new QComboBox(); // make combo  
            ui.tableView_2->setIndexWidget(idx, combo);// add combo
        }
        colvallist1.removeDuplicates(); // clear duplicates in colvallist1
        colvallist1.removeAll(QString("")); //remove empty row data
        combo1->setPlaceholderText(QString(" "))
        QString selected = combo1->currentText();
        int indx = combo1->currentIndex();
        combo1->clear();
        combo1->addItems(colvallist1);
        combo1->findData(selected);
        combo1->setCurrentIndex(indx);
        combo1->setCurrentText(selected); 
      }

【讨论】:

    猜你喜欢
    • 2021-05-20
    • 1970-01-01
    • 2013-07-23
    • 2021-04-12
    • 2020-04-10
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    相关资源
    最近更新 更多