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 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 )
-
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:
- 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:
on the background is my code in QtCreator and in the foreground there is that popup.
- 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(); } }
-
-
@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:And... with no solution
-
-
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:
-
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().
-
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(); } }
-
-
@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 :)