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. Editable QComboBox: when an item is selected and Return is pressed on it, how to move this item to the top?

Editable QComboBox: when an item is selected and Return is pressed on it, how to move this item to the top?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 3.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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    I'm handling returnPressed() signal of the QComboBox's QLineEdit. If it was an activation of the existing combobox item and not a creation of a new one, I want to move that item to the top of the combobox. What's the easiest way to do that?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      The easiest way I can see is to use your own model and move the entries around as needed

      Hope it helps

      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
      0
      • V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #3

        Aww, I have a painful and unproductive history of overriding default models. Anyway, thanks for the advice, hopefully combobox model is simpler than that for a QTreeView :)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          It is, you can use something like e.g. QAbstractListModel

          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
          0
          • V Offline
            V Offline
            Violet Giraffe
            wrote on last edited by
            #5

            Where can I find the list of methods I must implement for my model to work with QCombobox? Like which of the insert...() methods must I implement and which are not used by QCombobox?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Thinking about it again. You don't even need to subclass anything. Just use the currentModel. Add a row at the first place with the new content and remove the old one

              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
              0
              • V Offline
                V Offline
                Violet Giraffe
                wrote on last edited by
                #7

                Turns out it's even simpler, just these 4 lines in a slot subscribed to returnPressed():

                @const QString item = itemText(currentIndex());
                removeItem(currentIndex());
                insertItem(0, item);
                setCurrentIndex(0);@

                P. S. At which point is QLineEdit constructed? It's null in QCombobox constructor, and setLineEdit is not virtual. I wonder if I can find a place to subscribe to QLineEdit from within QCombobox.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Also yes, I've over engineered the thing a bit :D

                  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
                  0

                  • Login

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