Hi all,
How can i hav different delegates in different columns of a table. for instance, i want spinbox delegate in 2nd column and combobox in 4 column.
i took an example of spinbox delegate from examples directories and tried to do the changes:
/*
main.cpp
A simple example that shows how a view can use a custom delegate to edit
data obtained from a model.
*/
#include <QApplication>
#include <QHeaderView>
#include <QItemSelectionModel>
#include <QStandardItemModel>
#include <QTableView>
#include <QObject>
#include "delegate.h"
int main(int argc, char *argv[])
{
tableView.setModel(&model);
SpinBoxDelegate delegate;
ComboBoxDelegate comDel;
for (int row = 0; row < 4; ++row)
{
for (int column = 0; column < 1; ++column)
{
tableView.setItemDelegate(&comDel);
}
for (int column = 1; column < 2; ++column)
{
tableView.setItemDelegate(&delegate);
}
}
tableView.
setWindowTitle(QObject::tr("Spin Box Delegate"));
tableView.show();
return app.exec();
}
/*
main.cpp
A simple example that shows how a view can use a custom delegate to edit
data obtained from a model.
*/
#include <QApplication>
#include <QHeaderView>
#include <QItemSelectionModel>
#include <QStandardItemModel>
#include <QTableView>
#include <QObject>
#include "delegate.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStandardItemModel model(4, 2);
QTableView tableView;
tableView.setModel(&model);
SpinBoxDelegate delegate;
ComboBoxDelegate comDel;
for (int row = 0; row < 4; ++row)
{
for (int column = 0; column < 1; ++column)
{
tableView.setItemDelegate(&comDel);
QModelIndex index = model.index(0, 0, QModelIndex());
}
for (int column = 1; column < 2; ++column)
{
tableView.setItemDelegate(&delegate);
QModelIndex index = model.index(1, 1, QModelIndex());
}
}
tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));
tableView.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
/*
delegate.cpp
A delegate that allows the user to change integer values from the model
using a spin box widget.
*/
#include <QtGui>
#include "delegate.h"
SpinBoxDelegate
::SpinBoxDelegate(QObject *parent
){
}
{
editor->setMinimum(0);
editor->setMaximum(100);
editor->installEventFilter(const_cast<SpinBoxDelegate*>(this));
return editor;
}
void SpinBoxDelegate
::setEditorData(QWidget *editor,
{
int value = index.model()->data(index, Qt::DisplayRole).toInt();
QSpinBox *spinBox
= static_cast<QSpinBox
*>
(editor
);
spinBox->setValue(value);
}
{
QSpinBox *spinBox
= static_cast<QSpinBox
*>
(editor
);
spinBox->interpretText();
int value = spinBox->value();
model->setData(index, value);
}
void SpinBoxDelegate
::updateEditorGeometry(QWidget *editor,
{
editor->setGeometry(option.rect);
}
/***********************************************************************************************/
ComboBoxDelegate
::ComboBoxDelegate(QObject *parent
){
}
{
list << "ankur" << "mitesh";
editor->addItems(list);
editor->installEventFilter(const_cast<ComboBoxDelegate*>(this));
return editor;
}
void ComboBoxDelegate
::setEditorData(QWidget *editor,
{
QString value
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
QComboBox *comboBox
= static_cast<QComboBox
*>
(editor
);
comboBox->addItem(value);
}
{
QComboBox *comboBox
= static_cast<QComboBox
*>
(editor
);
QString value
= comboBox
->currentText
();
model->setData(index, value);
}
void ComboBoxDelegate
::updateEditorGeometry(QWidget *editor,
{
editor->setGeometry(option.rect);
}
/***********************************************************************************************/
/*
delegate.cpp
A delegate that allows the user to change integer values from the model
using a spin box widget.
*/
#include <QtGui>
#include "delegate.h"
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMinimum(0);
editor->setMaximum(100);
editor->installEventFilter(const_cast<SpinBoxDelegate*>(this));
return editor;
}
void SpinBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
int value = index.model()->data(index, Qt::DisplayRole).toInt();
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
}
void SpinBoxDelegate::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);
}
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
/***********************************************************************************************/
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
QStringList list ;
list << "ankur" << "mitesh";
editor->addItems(list);
editor->installEventFilter(const_cast<ComboBoxDelegate*>(this));
return editor;
}
void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->addItem(value);
}
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString value = comboBox->currentText();
model->setData(index, value);
}
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
/***********************************************************************************************/
To copy to clipboard, switch view to plain text mode
#ifndef DELEGATE_H
#define DELEGATE_H
#include <QItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QSpinBox>
{
Q_OBJECT
public:
SpinBoxDelegate
(QObject *parent
= 0);
void updateEditorGeometry
(QWidget *editor,
};
{
Q_OBJECT
public:
ComboBoxDelegate
(QObject *parent
= 0);
void updateEditorGeometry
(QWidget *editor,
};
#endif
#ifndef DELEGATE_H
#define DELEGATE_H
#include <QItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QSpinBox>
class SpinBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
SpinBoxDelegate(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;
};
class ComboBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
ComboBoxDelegate(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;
};
#endif
To copy to clipboard, switch view to plain text mode
is there somehwere i m wrong .....
Bookmarks