Skip to content
Snippets Groups Projects
Select Git revision
  • 58e3f544e8ca98299d3d8def1d066edd4ebeac3d
  • master default protected
  • new-money
  • dev
  • serial
  • plot
  • stast
  • acquisition-dialog
  • multiacc
  • v1.18
  • v1.17.4
  • v1.17.3
  • v1.17.2
  • v1.17.1
  • v1.17
  • v1.16
  • v1.15
  • v1.14.1
  • v1.14
  • v1.13.42
  • v1.13.41
  • v1.13
  • v1.12
  • v1.11
  • v1.10
  • v1.9
  • acquisition
  • v1.8
  • v1.7
29 results

comboboxitemdelegate.cpp

user avatar
Hugo Hörnquist authored
f944547f
History
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";
    }
}