mardi 9 mai 2017

Toggling a checkbox inside a QComboBox

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();
}




Aucun commentaire:

Enregistrer un commentaire