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. How to programmatically change the currentIndex of QTableView's comboBox. ?
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 3.2k 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.
  • A Offline
    A Offline
    Ahmed000001
    wrote on 30 Oct 2018, 22:44 last edited by
    #1

    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

    J 1 Reply Last reply 30 Oct 2018, 23:57
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 30 Oct 2018, 22:53 last edited by
      #2

      Hi,

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

      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
      • A Offline
        A Offline
        Ahmed000001
        wrote on 30 Oct 2018, 22:58 last edited by
        #3

        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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 30 Oct 2018, 23:10 last edited by
          #4

          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.

          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
          2
          • A Offline
            A Offline
            Ahmed000001
            wrote on 30 Oct 2018, 23:37 last edited by
            #5

            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

            1 Reply Last reply
            0
            • A Ahmed000001
              30 Oct 2018, 22:44

              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

              J Offline
              J Offline
              JKSH
              Moderators
              wrote on 30 Oct 2018, 23:57 last edited by
              #6

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

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              A 1 Reply Last reply 31 Oct 2018, 00:06
              0
              • J JKSH
                30 Oct 2018, 23:57

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

                A Offline
                A Offline
                Ahmed000001
                wrote on 31 Oct 2018, 00:06 last edited by
                #7

                @JKSH m_TableView->setItemDelegate(new ComboBoxItemDelegate);

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 31 Oct 2018, 20:07 last edited by
                  #8

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

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

                  A 1 Reply Last reply 31 Oct 2018, 20:21
                  0
                  • S SGaist
                    31 Oct 2018, 20:07

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

                    A Offline
                    A Offline
                    Ahmed000001
                    wrote on 31 Oct 2018, 20:21 last edited by
                    #9

                    @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

                    1 Reply Last reply
                    0

                    1/9

                    30 Oct 2018, 22:44

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved