#include <QtCore/QSize>
#include <QtCore/QModelIndex>
#include <QtGui/QStandardItemModel>
#include <QtWidgets/QApplication>
#include <QtWidgets/QStylePainter>
#include <QtWidgets/QWidget>
#include <QtWidgets/QAbstractItemView>
#include <QtWidgets/QStyleOptionViewItem>
#include <QtWidgets/QItemDelegate>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QCheckBox>
#define Sun_On 1;
#define Mon_On 2;
#define Tue_On 4;
#define Wed_On 8;
#define Thu_On 16;
#define Fri_On 32;
#define Sat_On 64;
#define Sun_Off 63;
#define Mon_Off 95;
#define Tue_Off 111;
#define Wed_Off 119;
#define Thu_Off 123;
#define Fri_Off 125;
#define Sat_Off 126;
{
Q_OBJECT
public:
CheckBoxListDelegate
(QObject *parent
);
virtual ~CheckBoxListDelegate();
) const Q_DECL_OVERRIDE;
void setCurrentValue(const quint8 val);
private slots:
void slot_changed(const QModelIndex& topLeft, const QModelIndex& bottomRight);
private:
quint8 m_value; // the current bit matrix value from the table
QVector<QStandardItem *> items;
};
#include <QtCore/QSize>
#include <QtCore/QModelIndex>
#include <QtGui/QStandardItemModel>
#include <QtWidgets/QApplication>
#include <QtWidgets/QStylePainter>
#include <QtWidgets/QWidget>
#include <QtWidgets/QAbstractItemView>
#include <QtWidgets/QStyleOptionViewItem>
#include <QtWidgets/QItemDelegate>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QCheckBox>
#define Sun_On 1;
#define Mon_On 2;
#define Tue_On 4;
#define Wed_On 8;
#define Thu_On 16;
#define Fri_On 32;
#define Sat_On 64;
#define Sun_Off 63;
#define Mon_Off 95;
#define Tue_Off 111;
#define Wed_Off 119;
#define Thu_Off 123;
#define Fri_Off 125;
#define Sat_Off 126;
class CheckBoxListDelegate : public QItemDelegate
{
Q_OBJECT
public:
CheckBoxListDelegate(QObject *parent);
virtual ~CheckBoxListDelegate();
QWidget *createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index
) const Q_DECL_OVERRIDE;
void setEditorData(QWidget *editor,const QModelIndex &index) const Q_DECL_OVERRIDE;
void setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex &index) const Q_DECL_OVERRIDE;
void setCurrentValue(const quint8 val);
private slots:
void slot_changed(const QModelIndex& topLeft, const QModelIndex& bottomRight);
private:
quint8 m_value; // the current bit matrix value from the table
QVector<QStandardItem *> items;
QComboBox *combo;
QStandardItemModel* model;
QStandardItem* item1;
QStandardItem* item2;
QStandardItem* item3;
QStandardItem* item4;
QStandardItem* item5;
QStandardItem* item6;
QStandardItem* item7;
};
To copy to clipboard, switch view to plain text mode
#include <QtCore/QDebug>
#include <QtCore/QVector>
#include <QtWidgets/QStyle>
#include <QtWidgets/QStyleOption>
#include <QtGui/QStandardItem>
#include <QtGui/QStandardItemModel>
#include <QtGui/QPainter>
#include "editdaysdelegate.hpp"
//==== Delegate implementation ====//
{
m_value = 0;
combo
= new QComboBox;
// the widget to be delegated
item1->setText("Sun");
item1->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item1->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(0, item1);
items.append(item1);
item2->setText("Mon");
item2->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item2->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(1, item2);
items.append(item2);
item3->setText("Tue");
item3->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item3->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(2, item3);
items.append(item3);
item4->setText("Wed");
item4->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item4->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(3, item4);
items.append(item4);
item5->setText("Thu");
item5->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item5->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(4, item5);
items.append(item5);
item6->setText("Fri");
item6->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item6->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(5, item6);
items.append(item6);
item7->setText("Sat");
item7->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item7->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(6, item7);
items.append(item7);
combo->setModel(model);
}
CheckBoxListDelegate::~CheckBoxListDelegate()
{
delete item1;
delete item2;
delete item3;
delete item4;
delete item5;
delete item6;
delete item7;
delete model;
}
void CheckBoxListDelegate::setCurrentValue(const quint8 val)
{
m_value = val;
}
{
Q_UNUSED(bottomRight)
quint16 row;
row = topLeft.row();
if ( item->checkState() == Qt::Unchecked )
{
switch ( item->row() )
{
case 0:
m_value = m_value & Sun_Off;
break;
case 1:
m_value = m_value & Mon_Off;
break;
case 2:
m_value = m_value & Tue_Off;
break;
case 3:
m_value = m_value & Wed_Off;
break;
case 4:
m_value = m_value & Thu_Off;
break;
case 5:
m_value = m_value & Fri_Off;
break;
case 6:
m_value = m_value & Sat_Off;
break;
}
}
else if ( item->checkState() == Qt::Checked )
{
switch ( item->row() )
{
case 0:
m_value = m_value |= Sun_On;
break;
case 1:
m_value = m_value |= Mon_On;
break;
case 2:
m_value = m_value |= Tue_On;
break;
case 3:
m_value = m_value |= Wed_On;
break;
case 4:
m_value = m_value |= Thu_On;
break;
case 5:
m_value = m_value |= Fri_On;
break;
case 6:
m_value = m_value |= Sat_On;
break;
}
}
}
{
Q_UNUSED(option)
Q_UNUSED(index)
// create check box as our editor
editor->setModel(model);
return editor;
}
{
//set editor data
QComboBox myEditor
= static_cast<QComboBox>
(editor
);
myEditor->itemDelegate()->setModelData(editor,model,index);
}
{
//get the value from the editor (CheckBox)
QCheckBox myEditor
= static_cast<QCheckBox>
(editor
);
bool value = myEditor->isChecked();
//set model data
QMap<int,QVariant> data;
data.insert(Qt::DisplayRole,myEditor->text());
data.insert(Qt::UserRole,value);
model->setItemData(index,data);
}
#include <QtCore/QDebug>
#include <QtCore/QVector>
#include <QtWidgets/QStyle>
#include <QtWidgets/QStyleOption>
#include <QtGui/QStandardItem>
#include <QtGui/QStandardItemModel>
#include <QtGui/QPainter>
#include "editdaysdelegate.hpp"
//==== Delegate implementation ====//
CheckBoxListDelegate::CheckBoxListDelegate(QObject *parent) : QItemDelegate(parent)
{
m_value = 0;
combo = new QComboBox; // the widget to be delegated
model = new QStandardItemModel; // model for the delegate
item1 = new QStandardItem;
item1->setText("Sun");
item1->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item1->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(0, item1);
items.append(item1);
item2 = new QStandardItem;
item2->setText("Mon");
item2->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item2->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(1, item2);
items.append(item2);
item3 = new QStandardItem;
item3->setText("Tue");
item3->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item3->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(2, item3);
items.append(item3);
item4 = new QStandardItem;
item4->setText("Wed");
item4->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item4->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(3, item4);
items.append(item4);
item5 = new QStandardItem;
item5->setText("Thu");
item5->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item5->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(4, item5);
items.append(item5);
item6 = new QStandardItem;
item6->setText("Fri");
item6->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item6->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(5, item6);
items.append(item6);
item7 = new QStandardItem;
item7->setText("Sat");
item7->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item7->setData(Qt::Unchecked, Qt::CheckStateRole);
model->insertRow(6, item7);
items.append(item7);
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), this, SLOT(slot_changed(const QModelIndex &, const QModelIndex &)));
combo->setModel(model);
}
CheckBoxListDelegate::~CheckBoxListDelegate()
{
delete item1;
delete item2;
delete item3;
delete item4;
delete item5;
delete item6;
delete item7;
delete model;
}
void CheckBoxListDelegate::setCurrentValue(const quint8 val)
{
m_value = val;
}
void CheckBoxListDelegate::slot_changed(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
Q_UNUSED(bottomRight)
quint16 row;
row = topLeft.row();
QStandardItem *item = items[row];
if ( item->checkState() == Qt::Unchecked )
{
switch ( item->row() )
{
case 0:
m_value = m_value & Sun_Off;
break;
case 1:
m_value = m_value & Mon_Off;
break;
case 2:
m_value = m_value & Tue_Off;
break;
case 3:
m_value = m_value & Wed_Off;
break;
case 4:
m_value = m_value & Thu_Off;
break;
case 5:
m_value = m_value & Fri_Off;
break;
case 6:
m_value = m_value & Sat_Off;
break;
}
}
else if ( item->checkState() == Qt::Checked )
{
switch ( item->row() )
{
case 0:
m_value = m_value |= Sun_On;
break;
case 1:
m_value = m_value |= Mon_On;
break;
case 2:
m_value = m_value |= Tue_On;
break;
case 3:
m_value = m_value |= Wed_On;
break;
case 4:
m_value = m_value |= Thu_On;
break;
case 5:
m_value = m_value |= Fri_On;
break;
case 6:
m_value = m_value |= Sat_On;
break;
}
}
}
QWidget* CheckBoxListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option)
Q_UNUSED(index)
// create check box as our editor
QComboBox *editor = new QComboBox(parent);
editor->setModel(model);
return editor;
}
void CheckBoxListDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{
//set editor data
QComboBox myEditor = static_cast<QComboBox>(editor);
myEditor->itemDelegate()->setModelData(editor,model,index);
}
void CheckBoxListDelegate::setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex &index) const
{
//get the value from the editor (CheckBox)
QCheckBox myEditor = static_cast<QCheckBox>(editor);
bool value = myEditor->isChecked();
//set model data
QMap<int,QVariant> data;
data.insert(Qt::DisplayRole,myEditor->text());
data.insert(Qt::UserRole,value);
model->setItemData(index,data);
}
To copy to clipboard, switch view to plain text mode
Bookmarks