| 115 |
david |
1 |
#include <QtGui>
|
|
|
2 |
#include "textdelegate.h"
|
|
|
3 |
|
|
|
4 |
/*
|
|
|
5 |
textdelegate.cpp
|
|
|
6 |
|
|
|
7 |
A delegate that allows the user to change integer values from the model
|
|
|
8 |
using a line edit widget.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
textDelegate::textDelegate(int maxlen, QObject *parent)
|
|
|
12 |
: QItemDelegate(parent)
|
|
|
13 |
{
|
|
|
14 |
max_len = maxlen;
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
QWidget *textDelegate::createEditor(QWidget *parent,
|
|
|
18 |
const QStyleOptionViewItem &/* option */,
|
|
|
19 |
const QModelIndex &/* index */) const
|
|
|
20 |
{
|
|
|
21 |
QLineEdit *editor = new QLineEdit(parent);
|
|
|
22 |
editor->setMaxLength(max_len);
|
|
|
23 |
|
|
|
24 |
return editor;
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
void textDelegate::setEditorData(QWidget *editor,
|
|
|
28 |
const QModelIndex &index) const
|
|
|
29 |
{
|
|
|
30 |
QString value = index.model()->data(index, Qt::EditRole).toString();
|
|
|
31 |
|
|
|
32 |
QLineEdit *lineEditor = static_cast<QLineEdit*>(editor);
|
|
|
33 |
lineEditor->setText(value);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
void textDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
|
|
38 |
const QModelIndex &index) const
|
|
|
39 |
{
|
|
|
40 |
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
|
|
|
41 |
QString value = lineEdit->text();
|
|
|
42 |
|
|
|
43 |
model->setData(index, value, Qt::EditRole);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
void textDelegate::updateEditorGeometry(QWidget *editor,
|
|
|
47 |
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
|
|
|
48 |
{
|
|
|
49 |
editor->setGeometry(option.rect);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
|