![](/uploads/av-1168.jpg)
Студент
![*](style_images/cozy_green/pip.gif)
Группа: Участник
Сообщений: 93
Регистрация: 21.10.2009
Из: Нижний Новгород
Пользователь №: 1168
Спасибо сказали: 9 раз(а)
Репутация: 0
![](style_images/cozy_green/spacer.gif)
|
Вообщем, приблизительно сделал так: delegate.h : #ifndef DELEGATE_H
#define DELEGATE_H
#include <QtGui>
class UniversalDelegate : public QItemDelegate {
Q_OBJECT
public:
UniversalDelegate(QObject *parent = 0);
QWidget *createEditor(
QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor,
const QModelIndex &index) const;
void setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const;
void updateEditorGeometry(
QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
void setComboValuesForCell(const QModelIndex index,const QList< QPair<int,QString> > aValues);
void setComboValuesForColumn(const int aColumn,const QList< QPair<int,QString> > aValues);
void setColumnDelegate(const int aColumn,const int aDelegate) {aColumnDelegate[aColumn] = aDelegate;}
void setCellDelegate(const QModelIndex index,const int aDelegate) {aCellDelegate[index] = aDelegate;}
QList< QPair<int,QString> > getComboValuesForCell(const QModelIndex index) const;
int getCurrentDelegate(const QModelIndex index) const;
void Clear();
private slots:
void ChangeComboBoxText(int);
private:
QMap <QModelIndex,QList< QPair<int,QString> > > aValuesOfComboForCell;
QMap <QModelIndex,QVector<int> > aIndexesOfComboForCell;
QMap <int,QList< QPair<int,QString> > > aValuesOfComboForColumn;
QMap <int,QVector<int> > aIndexesOfComboForColumn;
QMap <int,int> aColumnDelegate;
QMap <QModelIndex,int> aCellDelegate;
};
#endif
delegate.cpp : #include "delegate.h"
UniversalDelegate::UniversalDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget *UniversalDelegate::createEditor(
QWidget *parent,
const QStyleOptionViewItem& ,
const QModelIndex& index) const {
int aCurUserRole;
aCurUserRole = aColumnDelegate.value(index.column(),-1);
if (aCurUserRole == -1)
aCurUserRole = aCellDelegate.value(index,0);
if (aCurUserRole==1)
{
QCheckBox *editor = new QCheckBox(parent);
connect(editor,SIGNAL(stateChanged(int)),this,SLOT(ChangeComboBoxText(int)));
return editor;
}
if (aCurUserRole==2)
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMinimum(0);
editor->setMaximum(100);
return editor;
}
if (aCurUserRole==3)
{
QComboBox *editor = new QComboBox(parent);
QList< QPair<int,QString> > aValues;
if (aColumnDelegate.contains(index.column()))
aValues = aValuesOfComboForColumn.value(index.column());
else
aValues = aValuesOfComboForCell.value(index);
QList< QPair<int,QString> >::const_iterator it=aValues.begin();
while (it != aValues.end())
{
const QPair <int,QString> aPair = *it;
editor->addItem(aPair.second);
++it;
}
return editor;
}
if (aCurUserRole==4)
{
QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
editor->setMinimum(0.00);
editor->setMaximum(999999999);
editor->setDecimals(2);
editor->setSingleStep(0.1);
return editor;
}
if (aCurUserRole==5)
{
QLineEdit *editor = new QLineEdit(parent);
return editor;
}
if (aCurUserRole==6)
{
QLineEdit *editor = new QLineEdit(parent);
return editor;
}
if (aCurUserRole==7)
{
QLineEdit *editor = new QLineEdit(parent);
editor->setReadOnly(true);
return editor;
}
QLineEdit *editor = new QLineEdit(parent);
return editor;
}
void UniversalDelegate::setEditorData(
QWidget *editor,
const QModelIndex &index) const {
int aCurUserRole;
aCurUserRole = aColumnDelegate.value(index.column(),-1);
if (aCurUserRole == -1)
aCurUserRole = aCellDelegate.value(index,0);
if (aCurUserRole==0)
{
if (index.model()->data(index, Qt::EditRole).isNull()) return;
const QString value = index.model()->data(
index, Qt::EditRole).toString();
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
lineEdit->setText(value);
}
if (aCurUserRole==1)
{
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
if (index.model()->data(index, Qt::EditRole).toInt()!=1)
{
checkBox->setChecked(false);
checkBox->setText(QString::fromUtf8("Нет"));
}
else
{
checkBox->setChecked(true);
checkBox->setText(QString::fromUtf8("Да"));
}
}
if (aCurUserRole==2)
{
const int value = index.model()->data(index, Qt::EditRole).toInt();
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
}
if (aCurUserRole==3)
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
const int value = index.model()->data(index, Qt::EditRole).toInt();
QVector<int> aIndexes;
if (aColumnDelegate.contains(index.column()))
aIndexes = aIndexesOfComboForColumn.value(index.column());
else
aIndexes = aIndexesOfComboForCell.value(index);
comboBox->setCurrentIndex(aIndexes.indexOf(value));
}
if (aCurUserRole==4)
{
const double value = index.model()->data(
index, Qt::EditRole).toDouble();
QDoubleSpinBox *doubleSpinBox = static_cast<QDoubleSpinBox*>(editor);
doubleSpinBox->setValue(value);
}
if (aCurUserRole==5)
{
if (index.model()->data(index, Qt::EditRole).isNull()) return;
const QString value = index.model()->data(
index, Qt::EditRole).toString();
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
lineEdit->setText(value);
}
if (aCurUserRole==6)
{
if (index.model()->data(index, Qt::EditRole).isNull()) return;
const QString value = index.model()->data(
index, Qt::EditRole).toString();
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
lineEdit->setText(value);
}
if (aCurUserRole==7)
{
return;
}
}
void UniversalDelegate::setModelData(
QWidget *editor,
QAbstractItemModel *model,
const QModelIndex& index) const {
int aCurUserRole;
aCurUserRole = aColumnDelegate.value(index.column(),-1);
if (aCurUserRole == -1)
aCurUserRole = aCellDelegate.value(index,0);
if (aCurUserRole==0)
{
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
model->setData(index, lineEdit->text());
}
if (aCurUserRole==1)
{
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
if (checkBox->isChecked())
model->setData(index, "1");
else
model->setData(index, "0");
}
if (aCurUserRole==2)
{
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->interpretText();
const int value = spinBox->value();
model->setData(index, value, Qt::EditRole);
}
if (aCurUserRole==3)
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QVector<int> aIndexes;
if (aColumnDelegate.contains(index.column()))
aIndexes = aIndexesOfComboForColumn.value(index.column());
else
aIndexes = aIndexesOfComboForCell.value(index);
model->setData(index, aIndexes.at(comboBox->currentIndex()), Qt::EditRole);
}
if (aCurUserRole==4)
{
QDoubleSpinBox *doubleSpinBox = static_cast<QDoubleSpinBox*>(editor);
doubleSpinBox->interpretText();
model->setData(index, doubleSpinBox->text());
}
if (aCurUserRole==5)
{
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
if (lineEdit->text().isEmpty())
{
model->setData(index, QVariant(QString::null));
return;
}
const QString aValue = QString::number(lineEdit->text().toDouble());
model->setData(index, aValue);
}
if (aCurUserRole==6)
{
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
if (lineEdit->text().isEmpty())
{
model->setData(index, QVariant(QString::null));
return;
}
const QString aValue = QString::number(lineEdit->text().toInt());
model->setData(index, aValue);
}
if (aCurUserRole==7)
{
return;
}
}
void UniversalDelegate::updateEditorGeometry(
QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex& ) const {
editor->setGeometry(option.rect);
}
void UniversalDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
int aCurUserRole;
aCurUserRole = aColumnDelegate.value(index.column(),-1);
if (aCurUserRole == -1)
aCurUserRole = aCellDelegate.value(index,0);
if (aCurUserRole==0 || aCurUserRole==2 || aCurUserRole==4 || aCurUserRole==5 || aCurUserRole==6)
{
if (option.state & QStyle::State_Editing)
return;
if (index.data(Qt::EditRole).isNull())
return;
const QString aText = index.data(Qt::EditRole).toString();
painter->drawText(
QRect(option.rect.left()+1,option.rect.top(),option.rect.width(),option.rect.hei
ght()),
Qt::AlignLeft | Qt::AlignVCenter,
aText
);
}
if (aCurUserRole==1)
{
if (option.state & QStyle::State_Selected)
{
return;
}
QString aText;
if (index.model()->data(index, Qt::EditRole).toInt()!=1)
aText = QString::fromUtf8("Нет");
else
aText = QString::fromUtf8("Да");
painter->drawText(
QRect(option.rect.left()+1,option.rect.top(),option.rect.width(),option.rect.hei
ght()),
Qt::AlignLeft | Qt::AlignVCenter,
aText
);
}
if (aCurUserRole==3)
{
if (option.state & QStyle::State_Editing)
return;
if (index.data(Qt::EditRole).isNull())
return;
QString aText;
const int aIntValue = index.data(Qt::EditRole).toInt();
QList< QPair<int,QString> > aValues;
if (aColumnDelegate.contains(index.column()))
aValues = aValuesOfComboForColumn.value(index.column());
else
aValues = aValuesOfComboForCell.value(index);
QList< QPair<int,QString> >::const_iterator it=aValues.begin();
while (it != aValues.end())
{
const QPair <int,QString> aPair = *it;
if (aPair.first == aIntValue)
{
aText = aPair.second;
break;
}
++it;
}
painter->drawText(
QRect(option.rect.left()+1,option.rect.top(),option.rect.width(),option.rect.hei
ght()),
Qt::AlignLeft | Qt::AlignVCenter,
aText
);
}
if (aCurUserRole==7)
{
return;
}
}
void UniversalDelegate::setComboValuesForCell(const QModelIndex index,const QList< QPair<int,QString> > aValues)
{
aValuesOfComboForCell[index] = aValues;
QVector<int> aIndexes;
QList< QPair<int,QString> >::const_iterator it=aValues.begin();
while (it != aValues.end())
{
const QPair <int,QString> aPair = *it;
aIndexes.append(aPair.first);
++it;
}
aIndexesOfComboForCell[index] = aIndexes;
}
void UniversalDelegate::setComboValuesForColumn(const int aColumn,const QList< QPair<int,QString> > aValues)
{
aValuesOfComboForColumn[aColumn] = aValues;
QVector<int> aIndexes;
QList< QPair<int,QString> >::const_iterator it=aValues.begin();
while (it != aValues.end())
{
const QPair <int,QString> aPair = *it;
aIndexes.append(aPair.first);
++it;
}
aIndexesOfComboForColumn[aColumn] = aIndexes;
}
QList< QPair<int,QString> > UniversalDelegate::getComboValuesForCell(const QModelIndex index) const
{
QList< QPair<int,QString> > aValues;
return aValuesOfComboForCell.value(index,aValues);
}
int UniversalDelegate::getCurrentDelegate(const QModelIndex index) const
{
return aCellDelegate.value(index,0);
}
void UniversalDelegate::Clear()
{
aValuesOfComboForCell.clear();
aIndexesOfComboForCell.clear();
aValuesOfComboForColumn.clear();
aIndexesOfComboForColumn.clear();
aCellDelegate.clear();
aColumnDelegate.clear();
}
void UniversalDelegate::ChangeComboBoxText(int aValue)
{
QCheckBox* aCheckBox = qobject_cast<QCheckBox*>(sender());
if (aValue != 0)
{
aCheckBox->setText(QString::fromUtf8("Да"));
}
else
aCheckBox->setText(QString::fromUtf8("Нет"));
}
Работает неплохо, при этом можно назначить делегат для ячейки или целого столбца. Один минус - не могу прорисовать делегат в неактивном состоянии. Вылетает программа, try ... catch не спасает. Поэтому например при прорисовке чекбокса в неактивном состоянии получаю просто текст Да или Нет, а сам флажок можно установить при редакции ячейки. В целом нормально...
|