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. QComboBox::addItem() clears any editable text
Forum Updated to NodeBB v4.3 + New Features

QComboBox::addItem() clears any editable text

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 2.1k Views 3 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.
  • J Online
    J Online
    JonB
    wrote on 25 Jul 2018, 12:57 last edited by JonB
    #1

    If you have an editable QComboBox, the user has typed some text in, the list of items is empty, and code then dynamically adds some new item(s) to the popup list, I find the line edit widget loses the previous text typed in.

    # Initialization
    cmb = QComboBox()
    cmb.setEditable(True)
    
    # Slot for re-populating list dynamically at a later time
    cmb.clear()         # this in itself does not affect/clear the line edit text
    cmb.addItem(...)    # this *does* clear the line edit text
    

    It appears it is the act of inserting the first item into an empty list which loses any edit text.

    That in turn seems to be because inserting the first item into an empty list auto-selects that item. When the popup appears I instantaneously see the first item there selected, and its text gets copied to the line edit. I have not seen this documented?

    I do not want this behaviour. I clear & re-populate the combobox when the user clicks on it. I need the list to be repopulated, but anything previously typed in should remain as-is.

    Is there any way to not have that first addItem() auto-select and replace the edit text? Otherwise I will have to write code to save the text, re-populate the list, and then re-set the text.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Jul 2018, 21:12 last edited by
      #2

      Hi,

      What version of Qt are you using ?
      On which platform ?

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

      J 1 Reply Last reply 26 Jul 2018, 06:52
      0
      • H Online
        H Online
        hskoglund
        wrote on 26 Jul 2018, 00:51 last edited by
        #3

        Hi, had same problem some time ago, it turns out that only the first addItem() after clear() is lethal i.e. zaps the editable text.

        So I resorted to saving/restoring (had only one place in my program were I did clear() on the combobox anyway), something like:

        # Slot for re-populating list dynamically at a later time
        auto savedText = cmb.currentText();
        cmb.clear()         # this in itself does not affect/clear the line edit text
        cmb.addItem(...)    # this *does* clear the line edit text
        cmb.setCurrentText(savedText);
        
        1 Reply Last reply
        3
        • S SGaist
          25 Jul 2018, 21:12

          Hi,

          What version of Qt are you using ?
          On which platform ?

          J Online
          J Online
          JonB
          wrote on 26 Jul 2018, 06:52 last edited by JonB
          #4

          @SGaist
          Sorry, I'm Qt 5.7 distro for Ubuntu 17.04.

          @hskoglund
          Thank you for confirming you have seen this behaviour too. Yes, as I said I realize I can manually save & restore just like your code. For now I will go do that. But perhaps someone like @SGaist will comment on whether this behaviour --- addItem() to an empty list, possibly just after clear(), auto-selects that item and copies its text over anything in the line edit --- is intentional/inevitable or accidental?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 29 Jul 2018, 08:37 last edited by
            #5

            Can you provide a minimal compilable example to reproduce the behaviour so we are sure we are all working on the same base ?

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

            J 1 Reply Last reply 30 Jul 2018, 06:32
            1
            • S SGaist
              29 Jul 2018, 08:37

              Can you provide a minimal compilable example to reproduce the behaviour so we are sure we are all working on the same base ?

              J Online
              J Online
              JonB
              wrote on 30 Jul 2018, 06:32 last edited by JonB
              #6

              @SGaist
              The following 5 lines produce the behaviour (Qt 5.7). No signals/slots required. Is this sufficient for a minimal example?

              cmb = QComboBox()
              cmb.setEditable(True)
              cmb.setCurrentText("Current text")
              cmb.clear()              # this in itself does not affect/clear the line edit text
              cmb.addItem("Item 1")    # this *does* clear the line edit text
              
              U 1 Reply Last reply 12 Dec 2024, 18:56
              0
              • J JonB
                30 Jul 2018, 06:32

                @SGaist
                The following 5 lines produce the behaviour (Qt 5.7). No signals/slots required. Is this sufficient for a minimal example?

                cmb = QComboBox()
                cmb.setEditable(True)
                cmb.setCurrentText("Current text")
                cmb.clear()              # this in itself does not affect/clear the line edit text
                cmb.addItem("Item 1")    # this *does* clear the line edit text
                
                U Offline
                U Offline
                Uwe Schneider
                wrote on 12 Dec 2024, 18:56 last edited by
                #7

                @JonB

                had a similar problem. addItem with a empty combo box wont work and it cleared the editable text (qt5.15 and mint 21.3). While searching for a solution, I stumbled upon this thread . After some digging I found a solution:

                When you block the signals before the clear() and enable them after the addItem() it works.

                cmb = QComboBox()
                cmb.setEditable(true)
                cmb.setCurrentText("Current text")
                cmb.blockSignals(true);
                cmb.clear()             
                cmb.addItem("Item 1")  
                cmb.blockSignals(false);
                
                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