Редакция 66 |
Редакция 77 |
К новейшей редакции |
Авторство |
Сравнить с предыдущей |
Последнее изменение |
Открыть журнал
| Скачать
| RSS
#include "delegateforclassestable.h"
#include <QtGui>
DelegateForClassesTable::DelegateForClassesTable(QObject *parent) :
QItemDelegate(parent)
{
items.append(tr("CHAR(10)"));
items.append(tr("CHAR(30)"));
items.append(tr("INT(10)"));
items.append(tr("DATA"));
}
QWidget *DelegateForClassesTable::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
//QSpinBox *editor = new QSpinBox(parent);
//editor->setMinimum(0);
//editor->setMaximum(100);
QComboBox *editor = new QComboBox(parent);
editor->addItems(items);
/*
editor->addItem("0");
editor->addItem("1");
editor->addItem("2");
editor->addItem("3");
editor->addItem("4");
editor->addItem("5");
editor->addItem("6");
editor->addItem("7");
editor->addItem("8");
editor->addItem("9");
// editor->addItem("0");
*/
return editor;
}
//! [1]
//! [2]
void DelegateForClassesTable::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
// int value = index.model()->data(index, Qt::EditRole).toInt();
// QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
// spinBox->setValue(value);
QString value = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
int curr_index = items.indexOf(value);
if (curr_index!= -1) comboBox->setCurrentIndex(curr_index);
else {
}
/*
if (value == "0") comboBox->setCurrentIndex(0);
if (value == "1") comboBox->setCurrentIndex(1);
if (value == "2") comboBox->setCurrentIndex(2);
if (value == "3") comboBox->setCurrentIndex(3);
if (value == "4") comboBox->setCurrentIndex(4);
if (value == "5") comboBox->setCurrentIndex(5);
if (value == "6") comboBox->setCurrentIndex(6);
if (value == "7") comboBox->setCurrentIndex(7);
if (value == "8") comboBox->setCurrentIndex(8);
if (value == "9") comboBox->setCurrentIndex(9);
*/
comboBox->setEditable(false);
//comboBox->setItemText(0, value);
}
//! [2]
//! [3]
void DelegateForClassesTable::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
// QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
// spinBox->interpretText();
// int value = spinBox->value();
// model->setData(index, value, Qt::EditRole);
QComboBox *comboBox = static_cast<QComboBox*>(editor);
int currIndex;
currIndex = comboBox->currentIndex();
QString value = comboBox->itemText(currIndex);
model->setData(index, value, Qt::EditRole);
}
//! [3]
//! [4]
void DelegateForClassesTable::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
//! [4]
void DelegateForClassesTable::setItems(QStringList new_items){
DelegateForClassesTable::items = new_items;
}