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 change QComboBox highlighted item when a particular signal is emitted

How to change QComboBox highlighted item when a particular signal is emitted

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.1k 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.
  • Q Offline
    Q Offline
    qt.1234
    wrote on last edited by
    #1

    Hi,

    How do i go about changing the highlighted item of the QComboBox's drop down list whenever a signal is emitted? Mouse event is disabled in my case. So by using setCurrentIndex(index), I could change the item in the ComboBox easily but the drop down list is not reflecting the change. Please advise. Thank you.

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

      Get the model index coresponding to the item i in the list: const QModelIndex idx = combobox->model()->index(i, combobox->modelColumn(), combobox->rootModelIndex());

      Select that index:
      combobox->view()->selectionModel()->select(idx, QItemSelectionModel::Select); (see https://doc.qt.io/qt-5/qitemselectionmodel.html#SelectionFlag-enum to customise how it gets selected)

      "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

      Q 1 Reply Last reply
      4
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #2

        You can use combobox->view()->selectionModel() to corol selected items in the dropdown

        "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

        Q 1 Reply Last reply
        3
        • VRoninV VRonin

          You can use combobox->view()->selectionModel() to corol selected items in the dropdown

          Q Offline
          Q Offline
          qt.1234
          wrote on last edited by qt.1234
          #3

          @VRonin
          Hi, Thank you for your reply. Do I use this?

          option 1:
          combobox->view()->selectionModel()->isSelected()->selectedRows(index);

          option 2:
          QModelIndex selectedItem;
          selectedItem.sibling(index,0);
          combobox->view()->selectionModel()->setCurrentIndex(selectedItem, QItemSelectionModel::Select);

          i have tried the above and it do not work. The drop down list is not reflecting the change, Did i do it right?

          JonBJ 1 Reply Last reply
          0
          • Q qt.1234

            @VRonin
            Hi, Thank you for your reply. Do I use this?

            option 1:
            combobox->view()->selectionModel()->isSelected()->selectedRows(index);

            option 2:
            QModelIndex selectedItem;
            selectedItem.sibling(index,0);
            combobox->view()->selectionModel()->setCurrentIndex(selectedItem, QItemSelectionModel::Select);

            i have tried the above and it do not work. The drop down list is not reflecting the change, Did i do it right?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #4

            @qt-1234
            You don't want the isSelected() in the middle, that just returns a bool. Similarly selectedRows() returns selected rows. There is a select() slot to do selection.

            1 Reply Last reply
            3
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #5

              Get the model index coresponding to the item i in the list: const QModelIndex idx = combobox->model()->index(i, combobox->modelColumn(), combobox->rootModelIndex());

              Select that index:
              combobox->view()->selectionModel()->select(idx, QItemSelectionModel::Select); (see https://doc.qt.io/qt-5/qitemselectionmodel.html#SelectionFlag-enum to customise how it gets selected)

              "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

              Q 1 Reply Last reply
              4
              • VRoninV VRonin

                Get the model index coresponding to the item i in the list: const QModelIndex idx = combobox->model()->index(i, combobox->modelColumn(), combobox->rootModelIndex());

                Select that index:
                combobox->view()->selectionModel()->select(idx, QItemSelectionModel::Select); (see https://doc.qt.io/qt-5/qitemselectionmodel.html#SelectionFlag-enum to customise how it gets selected)

                Q Offline
                Q Offline
                qt.1234
                wrote on last edited by
                #6

                @VRonin Thank you for your help! This works!

                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