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. How to refresh QComboBox?

How to refresh QComboBox?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 4.4k Views 2 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by TomNow99
    #1

    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();
        }
    }
    
    Pl45m4P 1 Reply Last reply
    1
    • T TomNow99

      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();
          }
      }
      
      Pl45m4P Online
      Pl45m4P Online
      Pl45m4
      wrote on last edited by
      #2

      @TomNow99

      Try update or repaint.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TomNow99
        wrote on last edited by
        #3

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

        Pl45m4P 1 Reply Last reply
        0
        • T TomNow99

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

          Pl45m4P Online
          Pl45m4P Online
          Pl45m4
          wrote on last edited by
          #4

          @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).


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TomNow99
            wrote on last edited by TomNow99
            #5

            @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

            Pl45m4P 1 Reply Last reply
            0
            • T TomNow99

              @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

              Pl45m4P Online
              Pl45m4P Online
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              @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.


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              0
              • T Offline
                T Offline
                TomNow99
                wrote on last edited by
                #7

                @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.

                mrjjM Pl45m4P 2 Replies Last reply
                0
                • T TomNow99

                  @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.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • T TomNow99

                    @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.

                    Pl45m4P Online
                    Pl45m4P Online
                    Pl45m4
                    wrote on last edited by
                    #9

                    @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()


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    1
                    • T Offline
                      T Offline
                      TomNow99
                      wrote on last edited by TomNow99
                      #10

                      @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();
                           }
                      }
                      
                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @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 :)

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        4

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved