where is my button? Problem with QComboBox
-
Hi,
I have very strange problem with QComboBox. I have a subclass of QComboBox, where I add QStandardItems to QStandardItemModel. Next I set this model. And the last thing is add QPushButton to first cell. When I click on this button I would like to see text "click" on qDebug. When I have only a few items everything is ok.
The problem is when I have many items in this comboBox ( many = I see slider when click on comboBox ).
This situation when there is error:
-
I click on comboBox's line Edit to show popup.
-
I move slider ( or using mouse wheel ) to don't see the first cell with QPushButton
-
click somewhere ( outside comboBox, click on comboBox's line Edit ) to hide popup
-
I click on comboBox's line Edit to show popup - there is no QPushButton
And picture:
- here I click the first time in comboBox's line Edit
- here I move slider to don't see the first cell
between 2) and 3) I click outside comboBox to hide popup - here I click the second time - there is no QPushButton ( I check clicking on first row - I don't see "click" )
My code:
In mainWindow I only add #include "mycombobox.h" and in costructor I only add linemyCombo = new myComboBox(this);
And myComboBox class:
#ifndef MYCOMBOBOX_H #define MYCOMBOBOX_H #include <QComboBox> #include <QStandardItem> #include <QStandardItemModel> class myComboBox:public QComboBox { Q_OBJECT public: myComboBox(QWidget *parent = nullptr); QStandardItemModel *itemModel; private slots: void buttonClick(); }; #endif // MYCOMBOBOX_H
#include "mycombobox.h" #include <QAbstractItemView> #include <QPushButton> #include <QDebug> myComboBox::myComboBox(QWidget *parent): QComboBox(parent) { QStringList textList; itemModel = new QStandardItemModel; textList<<""<<"a"<<"b"<<"c"<<"d"<<"e"<<"f"<<"g"<<"h"<<"i"<<"j"<<"k"<<"l"<<"m"<<"n"<<"o"<<"p"<<"r"<<"s"<<"t"; for(auto text: textList) { QStandardItem* item = new QStandardItem(text); itemModel->setItem(itemModel->rowCount(), 0, item); } setModel(itemModel); // add button to first cell QPushButton * button = new QPushButton; QModelIndex index = model()->index(0,0); connect(button, SIGNAL(clicked()), this, SLOT(buttonClick())); view()->setIndexWidget(index, button); } void myComboBox::buttonClick() { qDebug()<<"click"; }
-
-
Hi
For some reason, it gets a negative y pos.
QRect(0,-130 81x13)So it seems the scrolling moves it out of the viewports rect.
If i do
c->button->move(0,0);
(c is your combo)its back again in right place so it seems like a bug with QComboBox and setIndexWidget.
-
What, if you use
QPushButton
as delegate for the first item?@mrjj btw: I think
setIndexWidget
is buggy in general or can lead to some weird behavior. It's @VRonin 's best friend, according to the signature :DEdit:
setIndexWidget
documentation also says:This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QStyledItemDelegate instead.
(https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidget)
It says visible area... So I don't know, what happens, if you scroll and move the widget out of the visible area.
-
@Pl45m4
Hehe well its fair to say VRonin is not thrilled about setIndexWidget in general but its mostly due to being very heavy and
not the right way.But this issues does not happen with QListWidget (even if scrolling ) so I assume its due to how ComboBox works with its viewport.