PDA

View Full Version : QComboBox BUG?



mak_user
29th January 2012, 17:07
You can use this simple code if you want to check it out


#include <QtGui/QApplication>
#include <QComboBox>
#include <QStandardItemModel>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QStandardItemModel *model = new QStandardItemModel();
model->appendRow(new QStandardItem(QString("item1")));
model->appendRow(new QStandardItem(QString("item2")));
model->appendRow(new QStandardItem(QString("item3")));
model->appendRow(new QStandardItem(QString("item4")));


QComboBox *combo = new QComboBox();
combo->setModel(model);
combo->setEditable(true);
combo->setInsertPolicy(QComboBox::NoInsert);
combo->setMinimumWidth(100);

combo->show();

return a.exec();
}


To reproduce:
1.) Start this simple App and select item3 for example by moving down arrow key.
2.) Select full text "item3" in the combo either by double click or with shift and arrow keys.
3.) Press Enter.
You will see that after pressing Enter combo box is moving to previous set index, which in this case will be "item1".


Can I avoid this behaviour?

mak_user
29th January 2012, 23:19
Does anybody have any solution? I really need this.
I made my own system for tab ordering which responds to enter key. However, when user get into combo box and press enter value is changed, which is of course not appropriate behavior.
For the information, I am setting focus on the combo or any other widget with setFocus(Qt::TabFocusReason) and get the text inside selected.

wysota
29th January 2012, 23:44
How about not using a model? It doesn't make much sense to use a model with an editable combo anyway.

ChrisW67
30th January 2012, 01:20
I don't see that behaviour in the example program here: Qt 4.7.4 on Linux.

mak_user
30th January 2012, 12:23
@wysota: I use model cause I work with database tables(note: QSqlQueryModel which is not editable). I need editable combo only to use autocompleter from lineedit control inside the combo.

@ChrisW67: Do you redo the steps exactly as I said? Because in some cases it does work properly indeed(but rarely). I am using Qt 4.8.0, but I tested on 4.7.4 on Linux with the same result.

If you want I will put a small test project here with my tab order mechanism and combos, so you will easily reproduce.

Lykurg
30th January 2012, 13:46
I need editable combo only to use autocompleter from lineedit control inside the combo.
Huh?!? please elaborate... You can always create a QCompleter with a model yourself.

mak_user
30th January 2012, 14:45
I am already working on a project with lot of combos used like this. I would like to make it work this way by finding workaround over this strange behavior. This is small example of what is happening, attached in message.
Now, just press enter or return key and you will see value of combo2 changed.
You can change the value of any combo yourself by moving with up/down arrow key inside a combo and then continue clicking enter and you will see the same behavior.
What is strange if you set a value in the combo by using autocompleter(type the text item3 for example) and press enter then this value is remembered and tab ordering works fine on this field.
Do you understand my problem now?

mak_user
30th January 2012, 22:29
Why nobody even view my example? Am I doing something wrong or it is a QComboBox bug?

ChrisW67
30th January 2012, 23:14
@ChrisW67: Do you redo the steps exactly as I said? Because in some cases it does work properly indeed(but rarely). I am using Qt 4.8.0, but I tested on 4.7.4 on Linux with the same result.

I copied your code example, compiled it, run it, pressed the down arrow twice to select item3, pressed Enter. Item3 remained selected, exactly as expected. I repeated it with the mouse to select item 3: same result. Since you are doing something custom with Enter key presses I would start by looking there.


Why nobody even view my example? Am I doing something wrong or it is a QComboBox bug?
It could be that:

Not everyone is in your time zone: I certainly wasn't waiting at 00:45 this morning for your post.
Not everyone has unlimited time to debug other people's code.
Nobody cares ;)


A quick download and build of your code: no problem with either the value in the edited combo box or the change of focus.

I am wondering why you maintain a separate list of widgets to determine where next to put focus. Surely QWidget::nextInFocusChain() is an obvious candidate for this given that you have a pointer to the widget with the key press event.

mak_user
30th January 2012, 23:39
How do I set this chain?
I was using "void QWidget::setTabOrder ( QWidget * first, QWidget * second )" before this custom ordering, but there was a bug in it for sure when I have widget with child widget and set focusProxy on it. I can send you an example code of this too if you care ;) .

I am wondering however, how it is possible to work at your place and not at mine. I am using Qt4.7.4 on Linux to debug this.
Do you saying that when you press enter on second combo where "item4" is selected it doesn't revert value to "item1"?
This is strange. I have reproducible behavior here every time I start the application.
What I am doing is start my app and press enter several times and got all three combos set to "item1".

wysota
31st January 2012, 00:25
I am wondering however, how it is possible to work at your place and not at mine. I am using Qt4.7.4 on Linux to debug this.
Do you saying that when you press enter on second combo where "item4" is selected it doesn't revert value to "item1"?
This is strange. I have reproducible behavior here every time I start the application.
What I am doing is start my app and press enter several times and got all three combos set to "item1".

$ qmake -v | grep 4.
Using Qt version 4.7.4 in /usr/lib/x86_64-linux-gnu

If I just keep pressing Enter then I end up with all combos set to item1. If I choose an entry and press enter, it sticks properly. I wouldn't be suprised if this was caused by a non-editable model. And again, as I said earlier, I gave it a thought more than once and I claim that there is no sense in using a model with an editable combo unless you heavily customize the editing to avoid damaging the model.

mak_user
31st January 2012, 01:00
Thanks for taking time and testing it. I agree with you that that there is no sense in using a model with an editable combo. As I said before I only wished to use linedit inside combo to autocomplete text and I assumed that if I use non-editable model it will avoid editing and set combo to a previously set valid index which is not a case. It actually behaves unpredictable.
I open this thread only to get some suggestion to find a workaround. I will try to find another way.
Thanks for your help.

Added after 5 minutes:

BTW, can I determine if QWidget* points to QComboBox?

ChrisW67
31st January 2012, 01:13
Do you saying that when you press enter on second combo where "item4" is selected it doesn't revert value to "item1"?

No, I am saying that when I test it the way you asked it doesn't fail in the way you describe. You asked us to select a value on the combo then press enter. What you are doing is just pressing enter on the existing value.


What I am doing is start my app and press enter several times and got all three combos set to "item1".
Doing that does result in the second combo switching to item1.
Removing your event filter does not change this.
Allowing insertion does not change this.

Once a value has been selected through the GUI the problem no longer exists. This looks like an initialisation issue inside the QComboBox code.

If nobody else has a bright idea, search for a bug report in the Qt Bugtracker. If you don't find one then, using QTestLib, write an automated test that demonstrates the problem and lodge a bug report. Even better, find the actual fault.



BTW, can I determine if QWidget* points to QComboBox?

qobject_cast<>()

mak_user
31st January 2012, 02:13
I found workaround which will satisfy the user. I am checking if the widget is combo and call setFocus() instead of setFocus(Qt::TabFocusReason). In this case text is not highlighted and input doesn't perform.

Sintegrial
31st October 2015, 18:20
Maybe it can look like a necropost... but I can confirm that the issue is still present with Qt 5.4 on Windows. If any text is selected in an editable combobox then pressing on enter key while having focus on its line edit will result in index jumping to the 0 position...

tomancik
18th September 2020, 07:00
i Have a similar problem with comboboxes in our application. I am using QT 5.15 under Win10.
Our comboboxes are not editable, but if all text in the editfield is marked selected, and it is the text of an item with an index greater 0, the return-key selects the first item (instead of calling the default-pushbutton in modal dialogs f.e.). This was not the case with QT 5.7