fatecasino
28th February 2011, 18:04
Hi!
I have been studying the Spin Box Delegate Example (http://trinity.pearsoncomputing.net/docs/qt4/itemviews-spinboxdelegate.html) and I think I understood how it works.
Now I am trying to create the delegate for a qcombobox.
I have transformed all the SpinBox functions but I cannot do the same for the setEditorData().Any ideas?
#include <QtGui>
#include "delegate.h"
#include <delegate.h>
#include <comboboxdelegate.h>
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget *ComboBoxDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem("a");
editor->addItem("b56");
editor->addItem("cc");
return editor;
}
void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
// QString str = index.model()->data(index, Qt::EditRole);
// QComboBox *comboBox = static_cast<QComboBox*>(editor);
// comboBox->setValue(value);
// comboBox->setCurrentIndex(value);
}
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
//comboBox->interpretText();//is this important for the QComboBox delegate??
QString str = comboBox->currentText();
model->setData(index, str, Qt::EditRole);
}
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
I have been studying the Spin Box Delegate Example (http://trinity.pearsoncomputing.net/docs/qt4/itemviews-spinboxdelegate.html) and I think I understood how it works.
Now I am trying to create the delegate for a qcombobox.
I have transformed all the SpinBox functions but I cannot do the same for the setEditorData().Any ideas?
#include <QtGui>
#include "delegate.h"
#include <delegate.h>
#include <comboboxdelegate.h>
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget *ComboBoxDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem("a");
editor->addItem("b56");
editor->addItem("cc");
return editor;
}
void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
// QString str = index.model()->data(index, Qt::EditRole);
// QComboBox *comboBox = static_cast<QComboBox*>(editor);
// comboBox->setValue(value);
// comboBox->setCurrentIndex(value);
}
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
//comboBox->interpretText();//is this important for the QComboBox delegate??
QString str = comboBox->currentText();
model->setData(index, str, Qt::EditRole);
}
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}