Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Combobox in Qtableview
Forum Updated to NodeBB v4.3 + New Features

Combobox in Qtableview

Scheduled Pinned Locked Moved Solved General and Desktop
47 Posts 6 Posters 9.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N n-2204

    QStringList might be easier
    guesslist.removeDuplicates(); // strip dubs

     Ok Thanks!! but still looks like empty row value is coming in Combobox
    

    if you have many rows then setIndexWidget can become quite heavy for
    smaller devices and the app might lag

    I am having 6-7 rows in my table
    

    I'm not using delegate as I need to read index, selection and apply some condition based on Combobox values in other columns ...,for some further req.

    Thanks

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #26

    @n-2204

    Hi

    • Ok Thanks!! but still looks like empty row value is coming in Combobox

    Yes we add empty ones too but you can just dont do that.

    for (int r = 0, maxI = table1->rowCount(); r < maxI; ++r) { //for all value rows
       QString str = table1->data(table1->index(r, 0)).toString();
        if ( ! str.IsEmpty() ) 
         guesslist.append( str );//store value in stringlist
        }
    

    https://doc.qt.io/qt-5/qstring.html#isEmpty

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #27

      @mrjj said in Combobox in Qtableview:

      for (int i = 0, maxI = table2->rowCount(); i < maxI; ++i) { //for all rows
      QComboBox *combo = new QComboBox(); // make combo
      combo->addItems(guesslist); //add list to combobox
      ui->tableView_2->setIndexWidget(table2->index(i, 1), combo);// add combo
      }

      Slight tweak to avoid recreating the combobox every time:

      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());
      	combo->addItems(guesslist);  //add list to combobox
      }
      

      @n-2204 said in Combobox in Qtableview:

      I'm not using delegate as I need to read index, selection and apply some condition based on Combobox values in other columns

      You can do that with delegates too. Probably even easier.

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • N Offline
        N Offline
        n-2204
        wrote on last edited by n-2204
        #28

        @VRonin @mrjj
        1.
        Actually when I'm using both the ways to add values in Combobox
        one thing happening ,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

        I am using dataChanged() signal
        connect(ui.tableView->model(), &QAbstractItemModel::dataChanged, this,
        &tool::rowvalue1);
        Is it because of this signal ? how can i prevent this ??

        1. As I'm using SLOT::rowvalue1() to add combobox in Qtableview
          now how can i take index() of combobox as there is SIGNAL::QComboBox::currentIndexChanged, , so again I need to write SLOT for index
          void tool::IndexChanged(int index)
          {
          combo->setCurrentIndex(combo->currentIndex());
          //but combo will be inaccessible here ??
          }
          connect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
          &tool::IndexChanged);
          Is this correct way ?
        VRoninV 1 Reply Last reply
        0
        • N n-2204

          @VRonin @mrjj
          1.
          Actually when I'm using both the ways to add values in Combobox
          one thing happening ,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

          I am using dataChanged() signal
          connect(ui.tableView->model(), &QAbstractItemModel::dataChanged, this,
          &tool::rowvalue1);
          Is it because of this signal ? how can i prevent this ??

          1. As I'm using SLOT::rowvalue1() to add combobox in Qtableview
            now how can i take index() of combobox as there is SIGNAL::QComboBox::currentIndexChanged, , so again I need to write SLOT for index
            void tool::IndexChanged(int index)
            {
            combo->setCurrentIndex(combo->currentIndex());
            //but combo will be inaccessible here ??
            }
            connect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
            &tool::IndexChanged);
            Is this correct way ?
          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #29

          @n-2204 said in Combobox in Qtableview:

          Is it because of this signal ? how can i prevent this ??

          It's because you recreate the entire list every time. You should just add/remove only the items that changed


          You can use std::bind to forward the combo to the slot

          void IndexChanged(int index, QComboBox* combo){
          /// do stuff
          }
          

          connect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, std::bind(&tool::IndexChanged, this, std::placeholders::_1, combo));

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • N Offline
            N Offline
            n-2204
            wrote on last edited by n-2204
            #30

            @VRonin
            It's because you recreate the entire list every time. You should just add/remove only the items that changed
            okay,, ya I'm using dataChanged signal , so i think whenever changes in table1 col0 values so combobox selection got affect

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

            So need to change in SIGNAL or anycondition to be add?
            but I need to update data in combobox so datachanged signal must be used.
            need to give condition in combobox ? or in SLOT something need to change or what else ??

            VRoninV 1 Reply Last reply
            0
            • N n-2204

              @VRonin
              It's because you recreate the entire list every time. You should just add/remove only the items that changed
              okay,, ya I'm using dataChanged signal , so i think whenever changes in table1 col0 values so combobox selection got affect

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

              So need to change in SIGNAL or anycondition to be add?
              but I need to update data in combobox so datachanged signal must be used.
              need to give condition in combobox ? or in SLOT something need to change or what else ??

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #31

              Instead of just doing

              combo->model()->removeRows(0,combo->count(),combo->rootModelIndex()); // remove all items from the combo
              combo->addItems(guesslist);  //add list to combobox
              

              iterate over the items in the combo, remove those that don't appear in guesslist and add those that appear in guesslist but are not already in the combo

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • N Offline
                N Offline
                n-2204
                wrote on last edited by
                #32

                to remove duplicates

                guesslist.removeDuplicates(); // clear duplicates
                guesslist.removeAll(QString(""));
                combo->model()->removeRows(0,combo->count(),combo->rootModelIndex()); // remove all items from the combo
                combo->addItems(guesslist);  //add list to combobox
                

                remove those that don't appear in guesslist
                --means using removerows() all items are removed everytime so this need to change ??

                VRoninV 1 Reply Last reply
                0
                • N n-2204

                  to remove duplicates

                  guesslist.removeDuplicates(); // clear duplicates
                  guesslist.removeAll(QString(""));
                  combo->model()->removeRows(0,combo->count(),combo->rootModelIndex()); // remove all items from the combo
                  combo->addItems(guesslist);  //add list to combobox
                  

                  remove those that don't appear in guesslist
                  --means using removerows() all items are removed everytime so this need to change ??

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #33

                  @n-2204 said in Combobox in Qtableview:

                  all items are removed everytime so this need to change ??

                  Correct

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  N 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    @n-2204 said in Combobox in Qtableview:

                    all items are removed everytime so this need to change ??

                    Correct

                    N Offline
                    N Offline
                    n-2204
                    wrote on last edited by
                    #34

                    @VRonin Is removeItem() can be used ?

                    1 Reply Last reply
                    1
                    • N Offline
                      N Offline
                      n-2204
                      wrote on last edited by
                      #35

                      @VRonin said in Combobox in Qtableview:
                      for (int r = 0, maxI = table1->rowCount(); r < maxI; ++r) { //for all value rows
                      guesslist.append( table1->data(table1->index(r, 0)).toString() );//store value in stringlist
                      }

                      guesslist is always updated,

                      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
                              }
                              colvallist1.removeDuplicates(); // strip dubs clear dublicates
                              colvallist1.removeAll(QString(""));
                              combo->setPlaceholderText(QString(" "));
                      
                              combo->addItems(guesslist); // adding gueslist will add item value everytime so no need to pass this right ??
                      combo->removeItem(); 
                      

                      iterate over the items in the combo, remove those that don't appear in guesslist and add those that appear in guesslist but are not already in the combo

                      item should store in another Qstringlist and then check with initial list ?

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        n-2204
                        wrote on last edited by n-2204
                        #36

                        edited
                        In prev code i add these code lines, and now selection not changed when added new items in tableview column and updating in col combobox values

                         for (int i = 0; i < guesslist.count(); i++) 
                                {
                                    if (combo->findText(guesslist[i]) != -1)//-1 means "not found"
                                    { 
                                        continue;
                                    }
                                    else {
                                        combo->addItem(guesslist[i]); //Add to QCombobox
                                    }
                        

                        but not able to apply for "remove those t don't appear in guesslist (when col1 row values chnaged)" ??

                        JonBJ 1 Reply Last reply
                        0
                        • N n-2204

                          edited
                          In prev code i add these code lines, and now selection not changed when added new items in tableview column and updating in col combobox values

                           for (int i = 0; i < guesslist.count(); i++) 
                                  {
                                      if (combo->findText(guesslist[i]) != -1)//-1 means "not found"
                                      { 
                                          continue;
                                      }
                                      else {
                                          combo->addItem(guesslist[i]); //Add to QCombobox
                                      }
                          

                          but not able to apply for "remove those t don't appear in guesslist (when col1 row values chnaged)" ??

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #37

                          @n-2204
                          So you have to one of:

                          • Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now; or
                          • Clear out all the items in the combo box first, and then only add back in those which are now in guess list.
                          N 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @n-2204
                            So you have to one of:

                            • Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now; or
                            • Clear out all the items in the combo box first, and then only add back in those which are now in guess list.
                            N Offline
                            N Offline
                            n-2204
                            wrote on last edited by n-2204
                            #38

                            @JonB said in Combobox in Qtableview:

                            Clear out all the items in the combo box first, and then only add back in those which are now in guess list.

                            when i'm clearing all first then, whatever my prev selection in combobox it will be gone 
                             combo->clear();
                            

                            Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now

                            tried this but not working correctly where to chnge ?,when I'm changing value in guesslist then the combo selection got changed

                              QStringList itemsInComboBox;
                                        for (int index = 0; index < guesslist.count(); index++)
                                        {
                                            itemsInComboBox << combo->itemText(index);
                                            if ( guesslist[index]!=itemsInComboBox[index] )
                                                combo->removeItem(index); 
                                        }
                            
                            JonBJ 1 Reply Last reply
                            0
                            • N n-2204

                              @JonB said in Combobox in Qtableview:

                              Clear out all the items in the combo box first, and then only add back in those which are now in guess list.

                              when i'm clearing all first then, whatever my prev selection in combobox it will be gone 
                               combo->clear();
                              

                              Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now

                              tried this but not working correctly where to chnge ?,when I'm changing value in guesslist then the combo selection got changed

                                QStringList itemsInComboBox;
                                          for (int index = 0; index < guesslist.count(); index++)
                                          {
                                              itemsInComboBox << combo->itemText(index);
                                              if ( guesslist[index]!=itemsInComboBox[index] )
                                                  combo->removeItem(index); 
                                          }
                              
                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote on last edited by JonB
                              #39

                              @n-2204 said in Combobox in Qtableview:

                              when i'm clearing all first then, whatever my prev selection in combobox it will be gone
                              combo->clear();

                              Yes, and, so? If you want to preserve what was selected, note it and reselect.

                              Your loop for removing is no good. For one thing, you only check the same index in both guess list and combo box. If you do not clear the items, implement what I wrote:

                              Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now

                              That is just basic algorithmic stuff.

                              N 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @n-2204 said in Combobox in Qtableview:

                                when i'm clearing all first then, whatever my prev selection in combobox it will be gone
                                combo->clear();

                                Yes, and, so? If you want to preserve what was selected, note it and reselect.

                                Your loop for removing is no good. For one thing, you only check the same index in both guess list and combo box. If you do not clear the items, implement what I wrote:

                                Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now

                                That is just basic algorithmic stuff.

                                N Offline
                                N Offline
                                n-2204
                                wrote on last edited by n-2204
                                #40

                                @JonB

                                Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now

                                I tired this but not working correctly.. now if item in guesslist then also it got removed ???

                                 QStringList itemsInComboBox;
                                     for (int index = 0; index < guesslist.count(); ++index) {  //check in list
                                        for (int item = 0; item < combo->count(); ++item) {  //check in combobox
                                            itemsInComboBox << combo->itemText(item);
                                            qDebug() << "before remove " << itemsInComboBox;
                                                      if ( guesslist[index] != itemsInComboBox[item]  )
                                                      combo->removeItem(item);
                                            }
                                        }
                                
                                N 1 Reply Last reply
                                0
                                • N n-2204

                                  @JonB

                                  Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now

                                  I tired this but not working correctly.. now if item in guesslist then also it got removed ???

                                   QStringList itemsInComboBox;
                                       for (int index = 0; index < guesslist.count(); ++index) {  //check in list
                                          for (int item = 0; item < combo->count(); ++item) {  //check in combobox
                                              itemsInComboBox << combo->itemText(item);
                                              qDebug() << "before remove " << itemsInComboBox;
                                                        if ( guesslist[index] != itemsInComboBox[item]  )
                                                        combo->removeItem(item);
                                              }
                                          }
                                  
                                  N Offline
                                  N Offline
                                  n-2204
                                  wrote on last edited by
                                  #41

                                  Hi,
                                  adding those that appear in guesslist code-

                                  for (int i = 0; i < guesslist.count(); i++) 
                                      {
                                          if (combo->findText(guesslist[i]) != -1)//-1 means "not found"
                                          { 
                                              continue;
                                          }
                                          else {
                                              combo->addItem(guesslist[i]); //Add to QCombobox
                                          }
                                  

                                  ///remove which are not in the guess list code below I tried multiple times but this is not working correctly ? I don't want to change my selection when any item added / removed / replaced ..Plz help😞

                                  QStringList itemsInComboBox;
                                   for (int index = 0; index < guesslist.count(); ++index) {  //check in list
                                      for (int item = 0; item < combo->count(); ++item) {  //check in combobox
                                          itemsInComboBox << combo->itemText(item);
                                          qDebug() << "before remove " << itemsInComboBox;
                                                    if ( guesslist[index] != itemsInComboBox[item]  )
                                                    combo->removeItem(item);
                                          }
                                       }
                                  

                                  Thanks in advance

                                  1 Reply Last reply
                                  0
                                  • N Offline
                                    N Offline
                                    n-2204
                                    wrote on last edited by
                                    #42

                                    Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now

                                    for me this way is not working any how selection of combobox is going away..
                                    how can i store the selection of combobox ?

                                    artwawA JonBJ 2 Replies Last reply
                                    0
                                    • N n-2204

                                      Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now

                                      for me this way is not working any how selection of combobox is going away..
                                      how can i store the selection of combobox ?

                                      artwawA Offline
                                      artwawA Offline
                                      artwaw
                                      wrote on last edited by
                                      #43

                                      @n-2204 https://doc.qt.io/qt-5/qcombobox.html#currentIndex-prop or currentText() if you can't relay on the order of the items or itemData() if you use roles, later on you can find the same text with different index, if that's the case, using findText() or findData().

                                      Do you read any documentation at all? It's all there in clear text.

                                      For more information please re-read.

                                      Kind Regards,
                                      Artur

                                      N 1 Reply Last reply
                                      1
                                      • N n-2204

                                        Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now

                                        for me this way is not working any how selection of combobox is going away..
                                        how can i store the selection of combobox ?

                                        JonBJ Online
                                        JonBJ Online
                                        JonB
                                        wrote on last edited by JonB
                                        #44

                                        @n-2204
                                        The whole thing, for what I think you say you want:

                                        QString selected = combo->currentText();
                                        combo->clear();
                                        combo->addItems(guesslist);
                                        combo->setCurrentText(selected);
                                        
                                        N 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @n-2204
                                          The whole thing, for what I think you say you want:

                                          QString selected = combo->currentText();
                                          combo->clear();
                                          combo->addItems(guesslist);
                                          combo->setCurrentText(selected);
                                          
                                          N Offline
                                          N Offline
                                          n-2204
                                          wrote on last edited by n-2204
                                          #45

                                          @JonB said in Combobox in Qtableview:

                                          The whole thing, for what I think you say you want:

                                          QString selected = combo->currentText();
                                          combo->clear();
                                          combo->addItems(guesslist);
                                          combo->setCurrentText(selected);
                                          edited
                                          thanks it works,, there some other mistake in code

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved