Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to refresh QComboBox?



  • Hi,

    I have QComboBox ( subclass of QComboBox, but it is really standard QComboBox ) and QTimer with interval = 1 sec. When is timeout in QTimer I add to QComboBox new Item like this:

    addItem("randomText");
    

    When the QCombobox's popup is visible and I add new Item in that moment I see that item in comboBox, but I can select it only using scrollBar. Of course when I hide popup and show it everything is ok. Picture:

    popup.png

    1. popup was shown when timer add new Item. I see that there are new items, but I can select it only using scrollBar ( there is on the right )

    2. here I hide popup ans show it - here I see many items and don't need use scrollbar to select one item.

    I tried change timer slot to:

        addItem("randomText");
        showPopup();
    

    It refresh popup, but when I have hide popup I will have visible popup.

    So I tried something like ( timer slot ):

        addItem("randomText");
        if(view()->isVisible())
        {
            showPopup();
        }
    

    And here I find two other problems:

    1. Sometimes when I have visible popup and change focus on other app ( for example qt creator ) I still see popup ( only popup - no app, no comboBox ). Picture:
      strange.png

    on the background is my code in QtCreator and in the foreground there is that popup.

    1. Sometimes when I click many times on arrow in QComboBox I still see that ScrollBar ( and I don't talk about maxVisibleItems problem ).

    Is there better way to showPopup()?

    EDIT:
    My whole code:

    #include "mycombobox.h"
    #include <QTimer>
    #include <QAbstractItemView>
    
    myComboBox::myComboBox(QWidget *parent): QComboBox(parent)
    {
        QTimer *timer = new QTimer;
        connect(timer, &QTimer::timeout, this, &myComboBox::timeoutSlot);
        timer->start(1000);
        setMaxVisibleItems(40);
    }
    
    void myComboBox::timeoutSlot()
    {
        addItem("randomText");
        if(view()->isVisible())
        {
            showPopup();
        }
    }
    


  • @TomNow99

    Try update or repaint.



  • @Pl45m4 Thank you for answer. I tried, but with not result.



  • @TomNow99

    What if you update the QAbstrctItemView only?

    comboBox->view()->update();

    What you can also do is looking at Qt source code to see what happens in showPopup (how your popup view is created).



  • @Pl45m4 I tried many things:
    view(), view()->viewport(), this: update / repaint in many combinations. And no good result...

    EDIT:
    I find the same problem here:

    https://forum.qt.io/topic/99940/dynamically-fit-qcombobox-popup-height-to-the-dynamically-changing-count-of-items/9

    And... with no solution



  • @TomNow99

    Yeah, that's what I also thought ( @J-Hilk 's answer). You have to re-create the popup widget / the view, because as long as it stays open, the comboBox knows, that there are new items, but the view will keep its old size. So the scrollbar appears.



  • @Pl45m4 Do you know any way to check if view() is in state like this:

    strange.png

    ?

    When I check isVisible() I always get true.


  • Lifetime Qt Champion

    @TomNow99
    so
    combobox->view()->isVisible()
    always returns true ?



  • @TomNow99 said in How to refresh QComboBox?:

    check if view() is in state like this

    What state? You mean fully expanded (no scrollbar)?

    comboBox->view()->verticalScrollBar()->isVisible()



  • @mrjj @Pl45m4
    My plan:

    After add new item to comboBox I have to use showPopup() method. But when I have only this one function in my timer slot, popup will be shown when popup is hidden and when is visible. I don't want situation that I click somewhere in button to add new item to comboBox, this comboBox will be hidden and poof... now is visible :D

    So I have to check state - view()->isVisible.

    And now is ok. When I add new Item and popup is hidden - it will be hidden. When I add new item and popup is visible - I add new item and popup will be visible. Perfect.

    But I have 2 other problems:

    1. Sometimes when I click many times on arrow in QComboBox I still see that ScrollBar ( and I don't talk about maxVisibleItems problem ). I solve ( I think I solve ) problem override paint() function and in this function I check visibility of verticalScrollBar. When it is visible = showPopup().

    2. Sometimes when I have visible popup and change focus on other app ( for example qt creator ) I still see popup ( only popup - no app, no comboBox ) - picture to this situation is above in 2 places.

    I think when I click on other app (in this example qtCreator ) ) to hide popup, in my timer slot at the same moment I check visible of popup. It is in state "now I'm in process 'during hide popup' ". So popup is hide in 50%, but the state is still visible :D And popup is shown without my app. So my condition

    view()->isVisible() ?
    

    is not good.

    EDIT:

    My code is something like ( maybe be wrong, because I'm in other computer, so please treat this code like pseudocode ):

    #include "mycombobox.h"
    #include <QTimer>
    #include <QAbstractItemView>
    #include <QScrollBar>  
    
    myComboBox::myComboBox(QWidget *parent): QComboBox(parent)
    {
        QTimer *timer = new QTimer;
        connect(timer, &QTimer::timeout, this, &myComboBox::timeoutSlot);
        timer->start(1000);
        setMaxVisibleItems(40);
    }
    
    void myComboBox::timeoutSlot()
    {
        addItem("randomText");
        if(view()->isVisible())
        {
            showPopup();
        }
    }
    
    void myComboBox::paint(QPaintEvent *ev)
    {
         QComboBox::paint(ev);
         if(view()->verticalScrollBar()->isVisible())
         {
                showPopup();
         }
    }
    

  • Qt Champions 2019

    @TomNow99 said in How to refresh QComboBox?:

    When the QCombobox's popup is visible and I add new Item in that moment I see that item in comboBox, but I can select it only using scrollBar.

    See https://bugreports.qt.io/browse/QTBUG-39420 - feel free to provide a patch :)


Log in to reply