ne2208
ne2208

Reputation: 3

Qcombobox remove and add item in C++ Qt

Creating a combobox in Qtableview2 column 1 and passing values from Qtableview1 column1 so i am storing column1 table1 values in Qstringlist and passing to combobox

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

Using removerows() all items are removed everytime,,if I added some values in column0 table1 then select in combobox and again adding values in table1 Column0 that time combobox selection go away but if i don't use removerows() then when I'm adding new item to combobox then its adding multiple time like 2 values added then i add 2 more in table1 col then in combobox it become 4

So, how to add those that appear in colvallist1 but are not already in the combo & remove those that don't appear in colvallist1 ??

Thanks Plz help!!

Upvotes: 0

Views: 691

Answers (1)

ne2208
ne2208

Reputation: 3

thnks,,,i solved this.. to remove the items in combobox which are not in colvallist1..without change in selection here is the code--

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

Upvotes: 0

Related Questions