Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to programmatically change the currentIndex of QTableView's comboBox. ?



  • I have a tableView that has in its first column a comboBox that works as item delegete. The combo box contains QString values.
    When the user double clicks the comboBox item, the comboBox appears and when the user select a value (QString) from the dropDown list, the functions createEditor, setEditorData, setModelData, are called properly and do the required work. Some other work is achieved depending on the QComboBox current value (current string).
    My Problems is :
    I want to set the value (string ) of the QComboBox programmatically as if the comboBox was clicked by the user from the ui. The problem is that i have no handler to that comboBox to call the function that will set the value of the combobox
    2. if i have the handler to that combobox, which function should i call to set the combo box current text


  • Lifetime Qt Champion

    Hi,

    When exactly do you want to call setCurrentIndex on your combo box ?



  • for example, let us consider that the user will select "Item1" from the comboBox drop down list, according to "Item1" some work will be done.
    but i want to do the same work programatically, i.e. i pass "Item1" as an argument to the funciton that will set the comboBox value.
    And to answer your question directly, i want to call that function outside the 3 stated function i.e. in any instant during the program


  • Lifetime Qt Champion

    Since you have a table view and that combo box is showing an entry of it, you want in fact to update the model which should in turn update the combo box.



  • i have already tried to do so using the following code:

       ui->tableView->model()->setData ( ui->tableView->model()->index(0,0), "item1", Qt::EditRole);
    

    but this line made the item (combobox) at the index (0,0) to be edited to "item1" but the required work did not be achieved.
    What i mean by the required work is the work that is done in the setModelData function.
    The problem with the previous code is that the function setModelData is not being called when set the item data with model setData function, so how to progromatically set the value of the combobox to call to automatically call the setModelData function


  • Moderators

    @Ahmed000001 said in How to programmatically change the currentIndex of QTableView's comboBox. ?:

    I have a tableView that has in its first column a comboBox that works as item delegete.

    How did you add the combo box to the table view?



  • @JKSH m_TableView->setItemDelegate(new ComboBoxItemDelegate);


  • Lifetime Qt Champion

    Can you provide a minimal compilable code sample that illustrate your problem ?



  • @SGaist Here is the setModelData function that is called when an item from the combobox is selected from the UI:

    void ComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
        if (QComboBox* cb = qobject_cast<QComboBox*>(editor)){
             qDebug()<<"hello, i have been called from the setModelData";
        }
        else
            QStyledItemDelegate::setModelData(editor, model, index);
    }
    

    But when i tried to set the data of the combobox (that is found in index (0,0)) to "Item A" by the following:

    ui->tableView->model()->setData ( ui->tableView->model()->index(0,0), "Item A, Qt::EditRole);
    

    the combobox's value was set successfully, but the ComboBoxItemDelegate::setModelData function was not called, and this is my problem


Log in to reply