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



  • 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?


  • Lifetime Qt Champion

    Hi,

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



  • 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



  • 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?



  • 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 )



  • 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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.