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. Alphabetical insert policy with QComboBox doesn't work well with my numeric values
Forum Updated to NodeBB v4.3 + New Features

Alphabetical insert policy with QComboBox doesn't work well with my numeric values

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 7.3k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    frankiefrank
    wrote on last edited by
    #1

    I have an issue with my editable QComboBox. It contains numeric values (mostly, there's also some text).

    For example:
    0 (some explanation)
    1 (something else)
    2
    3
    ..
    10
    11
    12
    200

    Now when the user enters a new value like "26" I naturally want it to appear between 12 and 200. If I use @QComboBox::InsertAlphabetically@ as my insert policy, I get the 26 between the 2 and the 3.

    How can I change this behavior? Is there a point where I can intervene with a custom insertion policy?

    "Roads? Where we're going, we don't need roads."

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      AFAIK, QComboBox doesn't have built-in functionality for sorting numerical strings, so it doesn't have the insertion policy you want.

      Perhaps you can implement your own sorting function, then have your program insert the new value at the index you want?

      You can also submit a feature request for your insertion policy at http://bugreports.qt-project.org -- I think it's a very reasonable request, seeing as modern operating systems use that policy for sorting filenames.

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

      1 Reply Last reply
      0
      • F Offline
        F Offline
        frankiefrank
        wrote on last edited by
        #3

        Thanks, JKSH. The insertion happens in the editable combos when user presses Enter, so ideally I'd have to override the insertItem method to put it in my own - only problem with that is that the method is not virtual :)

        So I'm guessing I'd need to use eventfilter to actually catch the return and do my own insertion, and that is kind of a lot of code for this silly sort.

        "Roads? Where we're going, we don't need roads."

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          What you can do is use your own model for the combobox, instead of using the item based approach. Your model may be a QSortFilterProxyModel on top of one of the standard models. Then, all you'd need to do is reimplement the lessThan method of the QSortFilterProxyModel to make sure the sorting is correct.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            frankiefrank
            wrote on last edited by
            #5

            Thanks Andre - that's a good direction. But then, what "insertPolicy" do I set on my combo box to both allow user to insert using "Enter" AND not to force a specific position?

            "Roads? Where we're going, we don't need roads."

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Actually, I don't know. It would take a bit of experimentation what happens if you use your own model in combination with the insert policy. My guess is that it would not matter much (unless you use InsertAtCurrent, I'd think), but I did not try this myself.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                frankiefrank
                wrote on last edited by
                #7

                Thanks for the tip anyway! If I get some of that happening, I'll post here.

                "Roads? Where we're going, we don't need roads."

                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