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. Qtableview Combobox in columns
Forum Updated to NodeBB v4.3 + New Features

Qtableview Combobox in columns

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 2.9k Views 2 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.
  • VRoninV VRonin

    pass it to the delegate... just create a variable, a getter and a setter in the delegate:

    public:
    void setComboModel(QAbstractItemModel* mdl){m_comboModel =mdl;}
    QAbstractItemModel* comboModel() const {return m_comboModel ;}
    private:
    QAbstractItemModel* m_comboModel = nullptr;
    
    N Offline
    N Offline
    n-2204
    wrote on last edited by
    #5

    @VRonin in table2 combobox values should be from my table1 column 2.
    so how can i store/get that values and pass as combobox->setmodel()

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

      https://doc.qt.io/qt-5/qcombobox.html#modelColumn-prop

      just call setModelColumn()

      "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
        #7

        Not working
        the values in combobox should come from table1 column 2
        and in table2 column 1 and 2 comobox should create
        how to do this?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #8

          Hi,

          Please take the time to read the documentation of the classes you are using: QComboBox::setModelColumn.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

            Hi,
            Anyone one example/code of Qtableview Combobox in columns
            please share
            the values in combobox should come from table1 column 2
            and in table2 column 1 and 2 comobox should create
            Thanks

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #10
              #include <QApplication>
              #include <QDebug>
              #include <QStyledItemDelegate>
              #include <QTableView>
              #include <QStandardItemModel>
              #include <QHBoxLayout>
              #include <QComboBox>
              
              class ComboDelegate : public QStyledItemDelegate{
                  Q_DISABLE_COPY(ComboDelegate)
              public:
                  ComboDelegate(QObject* parent = nullptr)
                      :QStyledItemDelegate(parent)
                      ,m_comboModel(nullptr)
                      ,m_comboColumn(0)
                  {}
                  QAbstractItemModel *comboModel() const{return m_comboModel;}
                  void setComboModel(QAbstractItemModel *comboModel){m_comboModel = comboModel;}
                  int comboColumn() const{return m_comboColumn;}
                  void setComboColumn(int comboColumn){m_comboColumn = comboColumn;}
                  QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &) const override{
                      QComboBox* result = new QComboBox(parent);
                      if(m_comboModel){
                          result->setModel(m_comboModel);
                          result->setModelColumn(m_comboColumn);
                      }
                      result->setGeometry(option.rect);
                      return result;
                  }
                  void setEditorData(QWidget *editor, const QModelIndex &index) const override{
                      QComboBox* result = qobject_cast<QComboBox*>(editor);
                      result->setCurrentIndex(result->findData(index.data(),Qt::EditRole));
                  }
                  void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override{
                      QComboBox* result = qobject_cast<QComboBox*>(editor);
                      model->setData(index,result->currentData(Qt::EditRole),Qt::EditRole);
                  }
                  void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const override
                  {
                      editor->setGeometry(option.rect);
                  }
              private:
                  QAbstractItemModel* m_comboModel;
                  int m_comboColumn;
              };
              
              int main(int argc, char *argv[])
              {
                  QApplication app(argc,argv);
                  QWidget wid;
                  QAbstractItemModel* table1 = new QStandardItemModel(&wid);
                  table1->insertColumns(0,2);
                  table1->insertRows(0,10);
                  for(int i=0;i<table1->rowCount();++i){
                      table1->setData(table1->index(i,0),i+1);
                      table1->setData(table1->index(i,1),QChar('A'+i+1));
                  }
                  QAbstractItemModel* table2 = new QStandardItemModel(&wid);
                  table2->insertColumns(0,2);
                  table2->insertRows(0,10);
                  for(int i=0;i<table2->rowCount();++i){
                      table2->setData(table2->index(i,0),i+100);
                      table2->setData(table2->index(i,1),QChar('a'+i+1));
                  }
                  QAbstractItemModel* table3 = new QStandardItemModel(&wid);
                  table3->insertColumns(0,2);
                  table3->insertRows(0,10);
              
                  QTableView* table1View = new QTableView(&wid);
                  table1View->setModel(table1);
                  QTableView* table2View = new QTableView(&wid);
                  table2View->setModel(table2);
                  QTableView* table3View = new QTableView(&wid);
                  table3View->setModel(table3);
                  ComboDelegate* delegate1 = new ComboDelegate(table3);
                  delegate1->setComboModel(table1);
                  delegate1->setComboColumn(1);
                  ComboDelegate* delegate2 = new ComboDelegate(table3);
                  delegate2->setComboModel(table2);
                  delegate2->setComboColumn(0);
                  table3View->setItemDelegateForColumn(0,delegate1);
                  table3View->setItemDelegateForColumn(1,delegate2);
                  QHBoxLayout* lay = new QHBoxLayout(&wid);
                  lay->addWidget(table1View);
                  lay->addWidget(table2View);
                  lay->addWidget(table3View);
                  wid.show();
                  return app.exec();
              }
              
              

              "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
                #11

                Thanks
                Is there another way to add combobox in Qtableview using setcellwidget
                or its there for only tablewidget not tableview?

                JonBJ 1 Reply Last reply
                0
                • N n-2204

                  Thanks
                  Is there another way to add combobox in Qtableview using setcellwidget
                  or its there for only tablewidget not tableview?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #12

                  @n-2204
                  As you can see from docs setCellWidget() is QTableWidget only, not QTableView. Though the latter does have equivalent setIndexWidget() instead.

                  But I can warn you now that if you use setCellWidget() or setIndexWidget() at all @VRonin will come down on you like a ton of bricks (look at his sig!) ;-) Which is why he showed you code using QStyledItemDelegate instead... :)

                  N 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @n-2204
                    As you can see from docs setCellWidget() is QTableWidget only, not QTableView. Though the latter does have equivalent setIndexWidget() instead.

                    But I can warn you now that if you use setCellWidget() or setIndexWidget() at all @VRonin will come down on you like a ton of bricks (look at his sig!) ;-) Which is why he showed you code using QStyledItemDelegate instead... :)

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

                    @JonB ok so diff part is how to pass the other column values in combobox as using delegate it is easier

                    /*  for (int i = 0, maxI = ui.tableView_2->model()->rowCount();i <= maxI;++i)
                    {
                        QComboBox* combo = new QComboBox();
                        combo->addItem("value1");
                        combo->addItem("value2");
                        combo->addItem("value3");
                        ui.tableView_2->setIndexWidget(ui.tableView_2->model()->index(i, 1), combo);
                    }
                    
                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #14

                      ui.tableView_2->setIndexWidget

                      No God, NOOOOOOO!

                      "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
                      • nageshN Offline
                        nageshN Offline
                        nagesh
                        wrote on last edited by
                        #15

                        @n-2204 Refer to Spin Box Delegate Example in QT
                        https://doc.qt.io/qt-5/qtwidgets-itemviews-spinboxdelegate-example.html
                        same concept for Combobox as well

                        1 Reply Last reply
                        0
                        • VRoninV VRonin

                          ui.tableView_2->setIndexWidget

                          No God, NOOOOOOO!

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

                          @VRonin what i have shared code is working
                          ya thing is how to pass column value in that combobox like what you have shared in combodelegate example

                          VRoninV 1 Reply Last reply
                          0
                          • N n-2204

                            @VRonin what i have shared code is working
                            ya thing is how to pass column value in that combobox like what you have shared in combodelegate example

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

                            @n-2204 said in Qtableview Combobox in columns:

                            what i have shared code is working
                            ya thing is how to pass column value in that combobox like what you have shared in combodelegate example

                            what you shared is a shoebox with wheels and you are asking how to put an engine in it to turn it into a car.

                            The correct way to do it is with a delegate. I shared a full working example using a delegate

                            "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
                            1
                            • VRoninV VRonin

                              @n-2204 said in Qtableview Combobox in columns:

                              what i have shared code is working
                              ya thing is how to pass column value in that combobox like what you have shared in combodelegate example

                              what you shared is a shoebox with wheels and you are asking how to put an engine in it to turn it into a car.

                              The correct way to do it is with a delegate. I shared a full working example using a delegate

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

                              @VRonin 😃 ok thanks

                              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