Select Git revision
comboboxitemdelegate.cpp 1.69 KiB
#include "comboboxitemdelegate.h"
#include <QComboBox>
#include <QtCore>
#include <QTableView>
static QString get_index_string(int);
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
// TODO should the destructor actually do anything
ComboBoxItemDelegate::~ComboBoxItemDelegate() {}
// initialises initial data
QWidget* ComboBoxItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const
{
QComboBox* cb = new QComboBox(parent);
for (int i = 0; i < 3; i++)
cb->addItem(get_index_string(i));
return cb;
}
// Sets data when clicked
void ComboBoxItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (QComboBox* cb = qobject_cast<QComboBox*>(editor)) {
cb->setCurrentIndex(index.data().toInt());
} else
QStyledItemDelegate::setEditorData(editor, index);
}
// sets return data
void ComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (QComboBox* cb = qobject_cast<QComboBox*>(editor))
model->setData(index, cb->currentIndex(), Qt::EditRole);
else
QStyledItemDelegate::setModelData(editor, model, index);
}
// sets what's displayed when not in edit mode
QString ComboBoxItemDelegate::displayText(const QVariant &value, const QLocale& /*locale*/) const
{
return get_index_string(value.toInt());
}
static QString get_index_string(int index) {
switch (index) {
case 0:
return "Till salu";
case 1:
return "Ej till salu";
case 2:
return "Dold";
default:
return "ERROR";
}
}