PDA

View Full Version : Multi-selection Combo Box



EricF
5th November 2007, 15:48
Hi, I'm trying to implement a combo box that would allow more than one selection(for flag purpose). I want the combo box to display check boxes in it instead of plain strings.

I already got that part covered usung a delegate on the combo box's list view for painting.

Now I need to be able to check the check boxes from within the list view. I tried implementing createEditor in my item delegate but it doesn't get called.

One last question, I would like to display the combined flags in the combo box's edit like Flag1 | Flag2 | Flag4. Is it possible to do it instead of the last selected item ?

Here's my code for the delegate in case you're interested :


void AcuiMultiSelectionComboBoxItemDelegate::paint(
QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index ) const
{
QStyleOptionButton buttonStyle;
buttonStyle.text = index.model()->data(index, Qt::DisplayRole).toString();
buttonStyle.rect = option.rect;

qApp->style()->drawControl(
QStyle::CE_CheckBox,
&buttonStyle,
painter );
}

QWidget* AcuiMultiSelectionComboBoxItemDelegate::createEdit or(
QWidget* parent,
const QStyleOptionViewItem& option,
const QModelIndex& index ) const
{
QCheckBox* checkBox = new QCheckBox(
index.model()->data(index, Qt::DisplayRole).toString(),
parent );

return checkBox;
}

Feel free to give any comments, recommendation.

jpn
5th November 2007, 16:35
QxtCheckComboBox (http://libqxt.org/static/docs/classQxtCheckComboBox.html) ;)

pherthyl
5th November 2007, 16:48
You can use Qxt for your checkable combo boxes
http://www.libqxt.org/static/docs/classQxtCheckComboBox.html

wysota
5th November 2007, 17:49
If you want to avoid the above mentioned dependency, you can set a model on the combobox and make sure the model enables items to be checkable (like Designer does for setting properties that are sets of flags).

EricF
5th November 2007, 19:09
Thanks for all the suggestions, I didn't know libqxt. And thanks for a work around.

EricF
6th November 2007, 15:13
I tried the work around and discovered that I didn't have to draw the combo box myself, setting the flag and handling the Qt::CheckStateRole in data() method does the job for me and even better because the check box I get is nicer than the one I was drawing. I would have like to see it in the documentation though.

However, it does not seem to handle the checking itself. Clicking on the check box selects the string (Normal behavior) but does not check the check box. Why ?

Is there some extensive documentation on model/view framework to read so I can get some insight to use it ?

Edit: I'll probably end up using QxtCheckComboBox but for the sake of learning I try to do it by myself.

wysota
6th November 2007, 15:36
Mouse events of the popup are intercepted by the combobox. You need to remove the event filter from the combobox or apply your own to make sure the view gets the event.

mchara
7th November 2007, 07:15
hello,
there's another way - it's lots of work, but gives also lots of possibilities:
inheriting combobox that provides own tree/list view/widget

There's some work to do with showing view as popup, hiding it when it loses focus etc. but You have FULL control over a popup including i.e. hierarchical views(like in file dialogs), more than 1 column shown in dropdown item & other non standard goodies you can imagine... but as i said, there's number of things that are for free in combobox or QxtCheckComboBox and have to be programmed in inherited classes.