#include <QtGui>
#include <QApplication>
{
Q_OBJECT
public:
, mText(index.data().toString())
{
hBoxLayout->addWidget(mLineEdit,2);
hBoxLayout->addWidget(pushButton,1);
connect(pushButton, SIGNAL(clicked()), SLOT(updateText()));
mLineEdit->setText(mText);
}
public:
{
return mText;
}
signals:
void textChanged
(const QString & text
);
public slots:
{
mText = text;
mLineEdit->setText(text);
}
private slots:
void updateText(void)
{
mText = mLineEdit->text();
emit textChanged(mText);
}
private:
};
class StyledItemDelegate : public QStyledItemDelegate
{
public:
explicit StyledItemDelegate
(QObject * parent
= 0) : QStyledItemDelegate(parent)
{
;
}
{
return new Editor(index, parent);
}
{
return Editor(index, 0).sizeHint();
}
protected:
{
static_cast<Editor*>(editor)->setText(index.data().toString());
}
{
static_cast<Editor*>(editor)->setGeometry(option.rect);
}
{
model->setData(index, static_cast<Editor*>(editor)->text());
}
};
{
model->setRowCount(10);
model->setColumnCount(2);
for(int r = 0; r < model->rowCount(); r ++)
for(int c = 0; c < model->columnCount(); c++)
model
->setData
(model
->index
(r, c
),
QString("Item-%1,%2").
arg(r
).
arg(c
), Qt
::DisplayRole);
}
int main(int argc, char ** argv)
{
StyledItemDelegate delegate;
addData(&model);
treeView.setModel(&model);
treeView.setItemDelegate(&delegate);
treeView.setEditTriggers(treeView.DoubleClicked);
treeView.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QApplication>
class Editor : public QWidget
{
Q_OBJECT
public:
explicit Editor(const QModelIndex & index, QWidget * parent)
: QWidget(parent)
, mLineEdit(new QLineEdit)
, mText(index.data().toString())
{
QHBoxLayout * hBoxLayout = new QHBoxLayout(this);
QPushButton * pushButton = new QPushButton("Apply");
hBoxLayout->addWidget(mLineEdit,2);
hBoxLayout->addWidget(pushButton,1);
connect(pushButton, SIGNAL(clicked()), SLOT(updateText()));
mLineEdit->setText(mText);
}
public:
QString text(void) const
{
return mText;
}
signals:
void textChanged(const QString & text);
public slots:
void setText(const QString & text)
{
mText = text;
mLineEdit->setText(text);
}
private slots:
void updateText(void)
{
mText = mLineEdit->text();
emit textChanged(mText);
}
private:
QLineEdit * mLineEdit;
QString mText;
};
class StyledItemDelegate : public QStyledItemDelegate
{
public:
explicit StyledItemDelegate(QObject * parent = 0)
: QStyledItemDelegate(parent)
{
;
}
virtual QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
return new Editor(index, parent);
}
virtual QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
return Editor(index, 0).sizeHint();
}
protected:
virtual void setEditorData(QWidget * editor, const QModelIndex & index) const
{
static_cast<Editor*>(editor)->setText(index.data().toString());
}
virtual void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
static_cast<Editor*>(editor)->setGeometry(option.rect);
}
virtual void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{
model->setData(index, static_cast<Editor*>(editor)->text());
}
};
void addData(QStandardItemModel * model)
{
model->setRowCount(10);
model->setColumnCount(2);
for(int r = 0; r < model->rowCount(); r ++)
for(int c = 0; c < model->columnCount(); c++)
model->setData(model->index(r, c), QString("Item-%1,%2").arg(r).arg(c), Qt::DisplayRole);
}
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
QTreeView treeView;
QStandardItemModel model;
StyledItemDelegate delegate;
addData(&model);
treeView.setModel(&model);
treeView.setItemDelegate(&delegate);
treeView.setEditTriggers(treeView.DoubleClicked);
treeView.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks