Possible to have permanent, unselectable first item in QComboBox?
-
A look over the documentation came up with nothing, but, I'm looking for a way to basically have a 'title' for the a QComboBox that will always show, even when another value is selected. I'm essentially using it as a drop-down list of buttons so as soon as an item is clicked on it will send a signal, so the first index should never change.
I could implement this with event filtering and so on but if there's a simpler way, that'd be great.
-
@Qt_User72653
There are a couple of ways to do that, I'll list those who come to my mind:- Use a QPushButton and give it a menu (QPushButton::setMenu(QMenu* menu)) of QActions instead
- Set the ComboBox as editable, make the lineedit part read only and use
setText
- Subclass QComboBox and overwrite the PaintEvent
- Subclass QComboBox and change the behaviour any other way than with the paintevent
-
@Qt_User72653 Why do you want a "title" inside the combobox?
Isn't it quite confusing for the user if he/she selects a value, but instead of that value this "title" is shown?
I as user would think that it is simply not working. -
@jsulm It's not really the selected or clicked value that's important, because clicking on one of the items is going to open up a new window. It's basically a drop-down box of buttons, with the item displayed in the combobox being the header for the list of buttons.
Actually, now that I've reviewed it, a QPushButton with a menu is exactly what I need.
-
@VRonin said in Possible to have permanent, unselectable first item in QComboBox?:
I'll add: Use a model (like
QStandardItemModel
) viaQComboBox::setModel
and make sure theflag()
method of the model returnsQt::NoItemFlags
for the first itemOften a combobox starts with a first item like "Pick one of the following". Presumably this would be just the right/easy way to implement that?