checkablecomboboxmodel.cpp 838 Bytes
Newer Older
Giacomo Strangolino's avatar
Giacomo Strangolino committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "checkablecomboboxmodel.h"
#include <QStandardItem>


#include <QtDebug>

CheckableComboBoxModel::CheckableComboBoxModel(QObject *parent) : QStandardItemModel(parent) {
    connect(this, SIGNAL(itemChanged(QStandardItem *)), this, SLOT(onItemChanged(QStandardItem *)));
}

QStandardItem* CheckableComboBoxModel::addItem(const QString &i, bool checkable)
{
    setRowCount(rowCount() + 1);
    QStandardItem* item = new QStandardItem(i);
    if(checkable) {
        item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item->setData(Qt::Unchecked, Qt::CheckStateRole);
    }
    setItem(rowCount() -1, 0, item);
    return item;
}

void CheckableComboBoxModel::onItemChanged(QStandardItem *it)
{
    qDebug() << __PRETTY_FUNCTION__ << it;
    emit itemChecked(it->text(), it->data(Qt::CheckStateRole).toBool());
}