PDA

View Full Version : Setting default item in a QCombobox



graffy
13th August 2013, 05:49
Hello,

I have a QComboBox that is tied to a model. I want to create a default text placeholder (without making it editable), so I added my placeholder text as the first item in the model. It shows up just fine, but I can't get it to set as the default selection in the QComboBox. At what point can I make the call to setCurrentIndex without the value being overwritten? The technique does work (qcb->setCurrentIndex (qcb->findText ("This is placeholder text..."));, but only if I insert it well after the dialog has been creted and shown... I've tried doing it in the show() slot, but with no success...

anda_skoa
13th August 2013, 11:03
If it works when the dialog is shown it might work in showEvent().

In any case, have you considered setting the placeholder text on the QComboBox line edit?
See QComboBox::lineEdit() and QLineEdit::setPlaceholderText()

Cheers,
_

graffy
13th August 2013, 13:50
Unfortunately, setPlaceHolder() works only for editable QComboBoxes. Strangely, if I do it with a non-editable box, I get a seg fault (Qt 4.8.5)...

ShowEvent() didn't work, either. It get's called before the widgets are shown and updated.

The only other solution I can come up with is to try refreshing the combo box on a timer triggered in ShowEvent(). It's sloppy, but it may work. I'd *much* rather hook into an event or something, though.

ChrisW67
13th August 2013, 21:57
The purpose of setCurrentIndex is to change the value, so "without the value being overwritten" makes little sense. Create the combo box, attach its model, and then call setCurrentIndex() (with zero as the argument if you know your placeholder is always the first entry in the model, or use your find if not). Where is the problem?

If you want something displayed on the combo box when nothing from the model is selected, i.e. currentIndex() returns -1, then that is a different request. I imagine you could do something in the paintEvent() function of a subclass for the case when the QStyleOptionComboBox current text is empty or currentIndex() is -1


class ComboBox: public QComboBox
{
Q_OBJECT
public:
explicit ComboBox(QWidget *p = 0): QComboBox(p) {
}
protected:
void paintEvent(QPaintEvent *) {
QStylePainter painter(this);
painter.setPen(palette().color(QPalette::Text));

// draw the combobox frame, focusrect and selected etc.
QStyleOptionComboBox opt;
initStyleOption(&opt);
painter.drawComplexControl(QStyle::CC_ComboBox, opt);

// draw the icon and text
if (!opt.editable && currentIndex() == -1) // <<< we adjust the text displayed when nothing is selected
opt.currentText = tr("{Select one}");
painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
}
};

The text is displayed but is not returned by currentText(), is not selectable by the user once a valid selection is made, and does not need to appear in your model. You should make the text configurable.

BTW: It is not strange that a combo box without an editor would return a null pointer when you ask for the editor. If you then proceeded to use the pointer blindly you will crash.

graffy
14th August 2013, 12:13
Still a bit new to Qt - I think I naturally expected the lineEdit to return a valid reference even if the combo box wasn't enabled - a side effect of working ActiveX (typically controls always exist, whether or not they're used)...

Thanks for the combobox code - that appears to be exactly what I needed!