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. Get the value of a combobox item delegate which is in a table
QtWS25 Last Chance

Get the value of a combobox item delegate which is in a table

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 4.4k Views
  • 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.
  • K Offline
    K Offline
    Koukoumaxe
    wrote on 15 Jan 2016, 08:13 last edited by
    #1

    Hello there !
    I have a table which has a column that is a combo box(Qitemdelegate). Now i need to be able to retrieve the chosen value of the combo box when the user presses a certain button.
    I googled all the possible combination but there aren't enough examples of combo boxes as item delegates.

    pricelists->setItemSize((ui->tblPricelist->model()->data(ui->tblPricelist->model()->index(results.size(),2)).toString()));
    

    This is what i use for getting the values from the table model. But if i'm not mistaken, the combo box isn't part of the model.

    Any ideas ?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 15 Jan 2016, 08:58 last edited by
      #2

      Hi
      Normally/often/as far as i know,
      the combo box would be an editor to set the data
      using a combo box (versus typing) so
      the selected value would be part of the model/data?
      User select value. Value stored for that row+col.

      Maybe show your Delegate code?

      K 1 Reply Last reply 15 Jan 2016, 09:03
      0
      • M mrjj
        15 Jan 2016, 08:58

        Hi
        Normally/often/as far as i know,
        the combo box would be an editor to set the data
        using a combo box (versus typing) so
        the selected value would be part of the model/data?
        User select value. Value stored for that row+col.

        Maybe show your Delegate code?

        K Offline
        K Offline
        Koukoumaxe
        wrote on 15 Jan 2016, 09:03 last edited by
        #3

        @mrjj I was actually looking that up.
        This is my current Delegate code :

        QtComboboxDelegate::QtComboboxDelegate(QStringList itemList, QObject *parent):itemList(itemList),
            QStyledItemDelegate(parent){
        
        }
        
        QWidget *QtComboboxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */,
                                                  const QModelIndex &/* index */) const{
        
          QComboBox* editor = new QComboBox(parent);
          for(unsigned int i = 0; i < itemList.size(); ++i){
        
            editor->addItem(itemList[i]);
        
          }
          return editor;
        }
        
        void QtComboboxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
          QComboBox *comboBox = static_cast<QComboBox*>(editor);
          int value = index.model()->data(index, Qt::EditRole).toInt();
          comboBox->setCurrentIndex(value);
        }
        
        void QtComboboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                           const QModelIndex &index) const
        {
            QComboBox *comboBox = static_cast<QComboBox*>(editor);
            model->setData(index, comboBox->currentIndex(), Qt::EditRole);
        }
        
        void QtComboboxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                                                    const QModelIndex &/* index */) const{
          editor->setGeometry(option.rect);
        }
        

        Similar to the delegate example made for the spinbox.

        My current thought is to create a getComboValue() method in this class.
        What is your thoughts ?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 15 Jan 2016, 09:10 last edited by mrjj
          #4

          @Koukoumaxe said:

          Hi, im still not sure why yo u need to talk to combo box directly. :)
          You call model->setData so after user selected something in combo box,
          that value should be in the model?
          So "the other place" could just get from model?

          QModelIndex index = model->index(row, col, QModelIndex());
          ui->view->model()->data(index).toString();

          1 Reply Last reply
          1
          • K Offline
            K Offline
            Koukoumaxe
            wrote on 15 Jan 2016, 09:14 last edited by
            #5

            oh ! so that is what you meant :)
            But then shouldn't my initial way work ?
            (i mean this :

            pricelists->setItemSize((ui->tblPricelist->model()->data(ui->tblPricelist->model()->index(results.size(),2)).toString()));
            

            )

            M 1 Reply Last reply 15 Jan 2016, 09:20
            0
            • K Koukoumaxe
              15 Jan 2016, 09:14

              oh ! so that is what you meant :)
              But then shouldn't my initial way work ?
              (i mean this :

              pricelists->setItemSize((ui->tblPricelist->model()->data(ui->tblPricelist->model()->index(results.size(),2)).toString()));
              

              )

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 15 Jan 2016, 09:20 last edited by
              #6

              @Koukoumaxe
              In theory yes, even its hard to say what
              ui->tblPricelist->model()->index(results.size(),2) becomes.

              But Im wondering if u need a emit DataChanged in setModelData but
              as far as i know would only affect the view and the model should have
              the combo box value.
              If you single step, the model->setData(index, comboBox->currentIndex()
              does set the index you are after?

              A 1 Reply Last reply 30 Oct 2018, 21:20
              0
              • K Offline
                K Offline
                Koukoumaxe
                wrote on 15 Jan 2016, 09:28 last edited by
                #7

                Well, your replies made me wonder enough to find where my mistake is !
                It doesn't have to do with the code mentioned above.
                That statement indeed gets the value from the Model.

                The problem is created because i add to the model in two different methods.
                That causes some logical errors at what the current index is e.t.c

                Therefore my problem isn't relevant to my initial question. Should i close the thread as SOLVED ?

                M 1 Reply Last reply 15 Jan 2016, 09:28
                1
                • K Koukoumaxe
                  15 Jan 2016, 09:28

                  Well, your replies made me wonder enough to find where my mistake is !
                  It doesn't have to do with the code mentioned above.
                  That statement indeed gets the value from the Model.

                  The problem is created because i add to the model in two different methods.
                  That causes some logical errors at what the current index is e.t.c

                  Therefore my problem isn't relevant to my initial question. Should i close the thread as SOLVED ?

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 15 Jan 2016, 09:28 last edited by
                  #8

                  @Koukoumaxe
                  Yes please do. Thank you

                  1 Reply Last reply
                  0
                  • M mrjj
                    15 Jan 2016, 09:20

                    @Koukoumaxe
                    In theory yes, even its hard to say what
                    ui->tblPricelist->model()->index(results.size(),2) becomes.

                    But Im wondering if u need a emit DataChanged in setModelData but
                    as far as i know would only affect the view and the model should have
                    the combo box value.
                    If you single step, the model->setData(index, comboBox->currentIndex()
                    does set the index you are after?

                    A Offline
                    A Offline
                    Ahmed000001
                    wrote on 30 Oct 2018, 21:20 last edited by
                    #9
                    This post is deleted!
                    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