PDA

View Full Version : [QComboBox] setEditable resets validator and completer



mentalmushroom
11th May 2012, 10:39
I've noticed every time editable state of QComboBox is changed, its validator and completer are set to 0. The same strange thing happens with the stylesheet: if you apply some custom style to editable combobox it won't work when you change editable state. I'd like to know if this weird behavior was intended or just something unnoticed by developers.

Spitfire
16th May 2012, 17:33
Styles seem to work fine for me.

Completer is removed because when you set editable to false the edit line is deleted (and completer with it).
The same happens for validator as both are children of a line edit which gets deleted when you set editable to false.

mentalmushroom
17th May 2012, 07:59
Completer is removed because when you set editable to false the edit line is deleted (and completer with it).
The same happens for validator as both are children of a line edit which gets deleted when you set editable to false.
Yes, I know why it happens, but I don't think it's a normal behavior, so just wanted to point it out, because it looks like a bug.

Stylesheet doesn't work correctly. Here goes a small sample that illustrates the issue. As you can see the background of the combobox should be blue, but it turns white (or default window color on your system) after making it editable. The issue is also caused by line edit deletion and when a new edit is created stylesheet is not applied. If I manually apply stylesheet again it works fine (see the commented line in the code).



#include <QApplication>
#include <QComboBox>

class MyComboBox: public QComboBox
{
Q_OBJECT

public:
MyComboBox(): QComboBox()
{
setStyleSheet("QComboBox { background-color: blue; }");
addItem("item 1");
addItem("item 2");
connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(handleNewIndex(int)));
}

private slots:
void handleNewIndex(int index)
{
setEditable(index % 2);
// after changing editable state we have to reapply stylesheet
//style()->polish(this);
}
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyComboBox cbox;
cbox.show();
return a.exec();
}

#include "main.moc"

Spitfire
17th May 2012, 11:42
You're right.
I've used border which works fine, but the background in fact is gone with the line edit.

I agree that it seem like some overlooked bug.

In fact there is an issue (https://bugreports.qt-project.org/browse/QTBUG-4322) about stylesheets but I can't find anything about completer or validator.
It may be worth rising it.