Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    2
    8
    2993
    Loading More Posts
    • 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
      Violet Giraffe last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • V
          Violet Giraffe last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • V
              Violet Giraffe last edited by

              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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • V
                  Violet Giraffe last edited by

                  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 Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    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 Reply Quote 0
                    • First post
                      Last post