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



  • 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 ?


  • Qt Champions 2016

    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?



  • @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 ?


  • Qt Champions 2016

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



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

    )


  • Qt Champions 2016

    @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?



  • 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 ?


  • Qt Champions 2016

    @Koukoumaxe
    Yes please do. Thank you


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.