跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  2. Qt Development
  3. General and Desktop
  4. Combobox in Qtableview
Forum Updated to NodeBB v4.3 + New Features

Combobox in Qtableview

已排程 已置頂 已鎖定 已移動 Solved General and Desktop
47 貼文 6 Posters 10.2k 瀏覽 3 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • N n-2204

    So when i am passing my values in a Qstring that time its coming in combobox but same when i try using table1 column1 rows value that time only last row value is adding in Combobox values looks like all values not appending
    Where need to make changes?

    connect(ui.tableView->model(), &QAbstractItemModel::dataChanged, this,
              &Gearcycle_model::changedvalue);
    
    void tool::changedvalue() {
    QAbstractItemModel* table1 = ui.tableView->model();
    QAbstractItemModel* table2 = ui.tableView_2->model();
    for (int i = 0, maxI = table2->rowCount();i <= maxI;++i)//combobox in table2 col1
    {
        for (int r = 0, maxI = table1->rowCount();r < maxI;++r)//getvalue fromtable1
        {
            QComboBox* combo = new QComboBox();
            //QString list1[10] = { table1->data(table1->index(r, 0)).toString() };//store value in list
            QString list1[4] = { "input 1","input 2","input3","input 4" };
            qDebug() << "table index" << r;
            for (int j = 0; j < 3; j++)
            {
                combo->addItem(list1[j]);
                qDebug() << "combox values" << list1[j];//pass list to combobox
                qDebug() << "list j=" << j;
            }
            ui.tableView_2->setIndexWidget(ui.tableView_2->model()->index(i, 1), combo);
            qDebug() << "row value i=" << i;
        }
    }
    }
    
    VRoninV 離線
    VRoninV 離線
    VRonin
    寫於 最後由 編輯
    #15

    @n-2204 said in Combobox in Qtableview:

    Where need to make changes?

    Read your code. The loops are wrong, this is basic C++

    "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 條回覆 最後回覆
    1
    • N n-2204

      So when i am passing my values in a Qstring that time its coming in combobox but same when i try using table1 column1 rows value that time only last row value is adding in Combobox values looks like all values not appending
      Where need to make changes?

      connect(ui.tableView->model(), &QAbstractItemModel::dataChanged, this,
                &Gearcycle_model::changedvalue);
      
      void tool::changedvalue() {
      QAbstractItemModel* table1 = ui.tableView->model();
      QAbstractItemModel* table2 = ui.tableView_2->model();
      for (int i = 0, maxI = table2->rowCount();i <= maxI;++i)//combobox in table2 col1
      {
          for (int r = 0, maxI = table1->rowCount();r < maxI;++r)//getvalue fromtable1
          {
              QComboBox* combo = new QComboBox();
              //QString list1[10] = { table1->data(table1->index(r, 0)).toString() };//store value in list
              QString list1[4] = { "input 1","input 2","input3","input 4" };
              qDebug() << "table index" << r;
              for (int j = 0; j < 3; j++)
              {
                  combo->addItem(list1[j]);
                  qDebug() << "combox values" << list1[j];//pass list to combobox
                  qDebug() << "list j=" << j;
              }
              ui.tableView_2->setIndexWidget(ui.tableView_2->model()->index(i, 1), combo);
              qDebug() << "row value i=" << i;
          }
      }
      }
      
      N 離線
      N 離線
      n-2204
      寫於 最後由 n-2204 編輯
      #16

      @n-2204 said in Combobox in Qtableview:

      So when i am passing my values in a Qstring that time its coming in combobox but same when i try using table1 column1 rows value that time only last row value is adding in Combobox values looks like all values not appending
      Where need to make changes?

      connect(ui.tableView->model(), &QAbstractItemModel::dataChanged, this,
                &Gearcycle_model::changedvalue);
      
      void tool::changedvalue() {
      QAbstractItemModel* table1 = ui.tableView->model();
      QAbstractItemModel* table2 = ui.tableView_2->model();
      for (int i = 0, maxI = table2->rowCount();i <= maxI;++i)//combobox in table2 col1
      {
          for (int r = 0, maxI = table1->rowCount();r < maxI;++r)//getvalue fromtable1
          {
              QComboBox* combo = new QComboBox();
              //QString list1[10] = { table1->data(table1->index(r, 0)).toString() };//store value in list
      

      when passing this that time only last row value is coming in combobox

              QString list1[4] = { "input 1","input 2","input3","input 4" };
      

      this is working when i am passing QString list1[4] = { "input 1","input 2","input3","input 4" }; in combobox

              for (int j = 0; j < 3; j++)
              {
                  combo->addItem(list1[j]);
                  qDebug() << "combox values" << list1[j];//pass list to combobox
                  qDebug() << "list j=" << j;
              }
              ui.tableView_2->setIndexWidget(ui.tableView_2->model()->index(i, 1), combo);
              qDebug() << "row value i=" << i;
          }
      }
      }
      

      yes, looks like some small change will be needed during addItem in combobox

      1 條回覆 最後回覆
      0
      • VRoninV 離線
        VRoninV 離線
        VRonin
        寫於 最後由 VRonin 編輯
        #17

        Look at the loops of i and r. think about what they are doing, ask yourself when you want to create and populate a combo, inside which loop?

        "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 條回覆 最後回覆
        1
        • N 離線
          N 離線
          n-2204
          寫於 最後由 n-2204 編輯
          #18

          ya it works thankyou.

          void tool::rowvalue1() {
          QAbstractItemModel* table1 = ui.tableView->model();
          QAbstractItemModel* table2 = ui.tableView_2->model();
          for (int i = 0, maxI = table2->rowCount();i < maxI;++i)//combobox in table2 col1
          {
               QComboBox* combo = new QComboBox();
                combo->setDuplicatesEnabled(false);
            for (int r = 0, maxI = table1->rowCount();r < maxI;++r)//getvalue fromtable1
                {
                   QString list1[10] = { table1->data(table1->index(r, 0)).toString() };//store value in list
                 for (int j = 0; j < 1; ++j)
                  {
          

          --using uniqueSet trying to add only unique values(no duplicate values) in Combobox but its not working is this correct way ? or any other way to do this ?

                      QSet<QString> uniqueSet;
                       if (!uniqueSet.contains(list1[j]))//add only unique values in combobox
                       {
                          uniqueSet.insert(list1[j]);
                          combo->addItem(list1[j]);  //add list to combobox  
                          qDebug() << " values " << list1[j];
                      }
                  }
                  ui.tableView_2->setIndexWidget(table2->index(i, 1), combo);
              }
            }
          }
          
          1 條回覆 最後回覆
          0
          • VRoninV 離線
            VRoninV 離線
            VRonin
            寫於 最後由 編輯
            #19

            @n-2204 said in Combobox in Qtableview:

            for (int j = 0; j < 1; ++j)

            What?!

            "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 條回覆 最後回覆
            2
            • N 離線
              N 離線
              n-2204
              寫於 最後由 n-2204 編輯
              #20

              no its 10
              j<10
              using uniqueSet trying to add only unique values(no duplicate values) in Combobox but its not working is this correct way ? or any other way to do this ?

              JonBJ 1 條回覆 最後回覆
              0
              • VRoninV 離線
                VRoninV 離線
                VRonin
                寫於 最後由 編輯
                #21

                My feeling is that you have no idea what your code does, you are just copy-pasting stuff around and hoping that it does something.
                Once again: read your code, run it step-by-step. This has nothing to do with Qt, it's just about loops

                "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 條回覆 最後回覆
                1
                • N 離線
                  N 離線
                  n-2204
                  寫於 最後由 編輯
                  #22

                  thanks for your answers
                  9f7df07c-9f26-4a5b-826b-00d285364dcb-image.png
                  but what i shared i working code
                  and now i asked -
                  using uniqueSet trying to add only unique values(no duplicate values) in Combobox but this way not working is this correct way ? or any other way to do this ?

                  this it might be very simple for you or you are expert but i am new C++ and Qt so i am having so many doubts may be silly.
                  Thanks

                  1 條回覆 最後回覆
                  0
                  • N n-2204

                    no its 10
                    j<10
                    using uniqueSet trying to add only unique values(no duplicate values) in Combobox but its not working is this correct way ? or any other way to do this ?

                    JonBJ 離線
                    JonBJ 離線
                    JonB
                    寫於 最後由 JonB 編輯
                    #23

                    @n-2204 said in Combobox in Qtableview:

                    no its 10
                    j<10

                    No, the code you pasted reads:

                     for (int j = 0; j < 1; ++j)
                    

                    That's 1, not 10. Or are you saying you want help to fix your code but you don't paste the actual code you want help with?

                    using uniqueSet trying to add only unique values(no duplicate values) in Combobox but this way not working

                    If your pasted code is to be believed, you seem to declare QSet<QString> uniqueSet; inside the j loop.

                                QSet<QString> uniqueSet;
                                 if (!uniqueSet.contains(list1[j]))//add only unique values in combobox
                    

                    This will always be true. uniqueSet is always empty.

                    1 條回覆 最後回覆
                    1
                    • mrjjM 離線
                      mrjjM 離線
                      mrjj
                      Lifetime Qt Champion
                      寫於 最後由 mrjj 編輯
                      #24

                      Hi

                      • any other way ?

                      Well your idea with QSet<QString> uniqueSet; was fine, but the logic/location was not right
                      and i think QStringList might be easier.

                      so this would do it. ( i assume you want a combo on each row in table 2 with same non-dub values in the combo)

                         QAbstractItemModel *table1 = ui->tableView->model();
                          QAbstractItemModel *table2 = ui->tableView_2->model();
                          QStringList guesslist;
                          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.removeDuplicates(); // strip dubs
                          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
                          }
                      

                      alt text

                      But a word of warning. If you have many rows then setIndexWidget can become quite heavy for
                      smaller devices and the app might lag. The cure is using a delegate as fully shown here ( thx VRonin :)

                      https://forum.qt.io/topic/125906/qtableview-combobox-in-columns/10

                      This also has the benefit that you don't need to copy anything around. its using the values directly. always updated.

                      1 條回覆 最後回覆
                      2
                      • N 離線
                        N 離線
                        n-2204
                        寫於 最後由 編輯
                        #25

                        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 1 條回覆 最後回覆
                        0
                        • 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 離線
                          mrjjM 離線
                          mrjj
                          Lifetime Qt Champion
                          寫於 最後由 編輯
                          #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 條回覆 最後回覆
                          0
                          • VRoninV 離線
                            VRoninV 離線
                            VRonin
                            寫於 最後由 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 條回覆 最後回覆
                            1
                            • N 離線
                              N 離線
                              n-2204
                              寫於 最後由 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 條回覆 最後回覆
                              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 離線
                                VRoninV 離線
                                VRonin
                                寫於 最後由 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 條回覆 最後回覆
                                1
                                • N 離線
                                  N 離線
                                  n-2204
                                  寫於 最後由 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 條回覆 最後回覆
                                  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 離線
                                    VRoninV 離線
                                    VRonin
                                    寫於 最後由 編輯
                                    #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 條回覆 最後回覆
                                    1
                                    • N 離線
                                      N 離線
                                      n-2204
                                      寫於 最後由 編輯
                                      #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 條回覆 最後回覆
                                      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 離線
                                        VRoninV 離線
                                        VRonin
                                        寫於 最後由 編輯
                                        #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 條回覆 最後回覆
                                        0
                                        • VRoninV VRonin

                                          @n-2204 said in Combobox in Qtableview:

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

                                          Correct

                                          N 離線
                                          N 離線
                                          n-2204
                                          寫於 最後由 編輯
                                          #34

                                          @VRonin Is removeItem() can be used ?

                                          1 條回覆 最後回覆
                                          1

                                          • 登入

                                          • Login or register to search.
                                          • 第一個貼文
                                            最後的貼文
                                          0
                                          • 版面
                                          • 最新
                                          • 標籤
                                          • 熱門
                                          • 使用者
                                          • 群組
                                          • 搜尋
                                          • Get Qt Extensions
                                          • Unsolved