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. Updated: QCombobox::currentText() returns wrong text after doing setCurrentIndex() followed by lineEdit()->selectAll()
Forum Update on Monday, May 27th 2025

Updated: QCombobox::currentText() returns wrong text after doing setCurrentIndex() followed by lineEdit()->selectAll()

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.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 have a class inherited from editable QComboBox that has a method for scrolling through combobox items:

    @void CHistoryComboBox::cycleLastItems()
    {
    if (!lineEdit() || count() == 0)
    return;

    if (lineEdit()->text().isEmpty())
    setCurrentIndex(0);
    else if (currentIndex() < count() - 1)
    setCurrentIndex(currentIndex() + 1);

    lineEdit()->selectAll();
    }@

    By default the combobox has some items, but neither is selected, so lineEdit()->text() returns empty string. Then I call cycleLastItems() once. I can now see the 1st item selected and lineEdit()->text() returns its name. But after I call cycleLastItems() again, I can see the 2nd item, but lineEdit()->text() still returns the first one. And so on - I will always get the first item's name, not the name I can actually see.

    Update: turns this only happens when I do lineEdit()->selectAll(). If I comment it everything works fine. What's the problem here?

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

      Hi,

      Can you post a minimal example that use your custom combo box ?

      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
      • A Offline
        A Offline
        arsinte_andrei
        wrote on last edited by
        #3

        if you can provide a bit mode code regarding how do you populate your combobox and also a bit of code around where are you reading the value from the comboboox. is by @ myCombobox->currentText(); @
        selectAll() as far as i remember do not move the pointer after it and any way this has nothing to do with combobox value... so a bit more of your code will help us better to reproduce the case

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Violet Giraffe
          wrote on last edited by
          #4

          I have created a new empty Qt Widgets project just to test this issue. I was able to reproduce it with no problem; without using my custom combobox. Just QComboBox. Unfortunately, I then became happy it's not just a bug of my big application and deleted the project instead of posting it here :(

          It is 100% clear that selectAll() resets currentIndex() to 0, which is the root cause of my problem. Could it be that selecting the text counts as altering the text, thus resetting current item index?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            arsinte_andrei
            wrote on last edited by
            #5

            then my advise is to hold the myCombobox->currentIndex(); in a variable and calling the selectAll and then just update this variable on user changing the combobox value /text - currentIndexChanged(int) and here save your variable and then when you need it again you have it here.
            do not forget to make it private or public into the h file (in order to be able to access it from any part of your source and from all functions )

            1 Reply Last reply
            0
            • V Offline
              V Offline
              Violet Giraffe
              wrote on last edited by
              #6

              OK, I nailed it when added some more stuff from my actual app where I noticed the problem. It's not as simple as I thought before, it involves doing setFocus() on the combobox. See the test project: https://drive.google.com/file/d/0B8Qc2atC6FVKckFWRV9vRXVaNUU/edit?usp=sharing

              Press F2 to scroll through items, that also selects the text and invokes setFocus() on the combobox. Then pressed Enter to see the current index. You'll see that combobox actually resets to item 0 and you'll see 0 for the current item index.

              In my app the combobox doesn't visually reset, but I think it's the same issue. That's why I got so confused.

              Update: if you comment ui->_btnCheck->setVisible(false) and uncomment connect() to this button, and use the button to get the currentIndex() instead of returnPressed() signal of the line edit, everything's fine. So, pressing Return is what causes the combobox to reset. How can I avoid that?
              I've just updated the test project link with new variation of the test.

              I think that since the text is selected, Enter clears it before sending returnPressed() signal. Can I avoid that?

              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