#include "typecomboboxdelegate.h"
#include <QComboBox>
#include <QApplication>
#include <QComboBox>
#include <QDebug>
TypeComBoboxDelegate::TypeComBoboxDelegate(QObject *parent):
m_combox_(new QComboBox())
{
m_type_list_ << QStringLiteral("char")
<< QStringLiteral("short")
<< QStringLiteral("int")
<< QStringLiteral("float")
<< QStringLiteral("double");
}
TypeComBoboxDelegate::~TypeComBoboxDelegate()
{
delete m_combox_;
}
QWidget *TypeComBoboxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItems(m_type_list_);
return editor;
}
void TypeComBoboxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString text = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
int tindex = comboBox->findText(text);
comboBox->setCurrentIndex(tindex);
}
void TypeComBoboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString text = comboBox->currentText();
model->setData(index, text, Qt::EditRole);
}
void TypeComBoboxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
//void TypeComBoboxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
//{
// QStyleOptionComboBox comboBoxOption;
// comboBoxOption.rect = option.rect;
// comboBoxOption.state = option.state;
// comboBoxOption.state |= QStyle::State_Enabled;
// comboBoxOption.editable = false;
// comboBoxOption.currentText = index.data().toString();
// QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
//}
评论0