0

I'm displaying checkboxes inside comboboxes using an adaptation of the following code. Unfortunately, the checkboxes do not behave exactly as expected. In contrast to a "normal" checkbox one can not click the checkbox's label to (un-)check the checkbox.

Is there a way to make the checkboxes inside the comboboxes behave exactly like a "normal" checkbox (i.e. clicking on the label (un-)checks it)?

#include <QtGui>
#include <QApplication>
#include <QComboBox>
#include <QTableView>
#include <QVBoxLayout>
#include <QListView>
#include <QCheckBox>

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

    QStandardItemModel model;
    QList<QStandardItem*> items;
    for (int i = 0; i < 5; i++) {
        QStandardItem* item = new QStandardItem(QString("Item %0").arg(i));
        item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item->setData(Qt::Unchecked, Qt::CheckStateRole);
        items.append(item);
    }

    model.appendColumn(items);

    QComboBox* comboBox = new QComboBox();
    comboBox->setModel(&model);

    QVBoxLayout* layout = new QVBoxLayout();
    layout->addWidget(comboBox);
    layout->addWidget(new QCheckBox("Label"));

    QWidget widget;
    widget.setLayout(layout);
    widget.show();

    return app.exec();
}
Jonas Möller
  • 363
  • 3
  • 16
  • 1
    Possible duplicate of [ComboBox of CheckBoxes?](http://stackoverflow.com/questions/8422760/combobox-of-checkboxes) – mohabouje May 09 '17 at 14:31
  • You have to subclass your own QItemDelegate and implement the paint event function. See: http://stackoverflow.com/questions/8422760/combobox-of-checkboxes – mohabouje May 09 '17 at 14:32

1 Answers1

0

No, you can not do that directly. However you can explicitly specify the behaviour of the label once it's clicked. Not to copy the code from another question, you can do something like in this question.

Community
  • 1
  • 1
tna0y
  • 1,842
  • 3
  • 16
  • 33