PDA

View Full Version : QLineEdit Delegate value update



yazwas
10th February 2010, 13:45
Hello All

I have created a QLineEdit delegate to add it to TreeView, the source code is done using the spinbox delegate example.
What I want is to get the value of that line edit box. I cant seem to get the correct value. I always get the one before the last value.

My questions is HOW to update the control so I can get the current value that the user presses Enter?

Below is the class for the LineEditDelegate, and also for the getValue slot in Mycalss



LineEditDelegate::LineEditDelegate(QObject *parent, Myclass* mc)
: QItemDelegate(parent), m_mc(mc)
{
}

QWidget *LineEditDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QLineEdit *editor = new QLineEdit(parent);

connect(editor, SIGNAL(returnPressed()), m_mc, SLOT(getValue() ) );

//connect(editor, SIGNAL(textChanged(QString)), editor, SLOT(setText(QString)) );

return editor;
}

void LineEditDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();

QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
lineEdit->setText(value);
}

void LineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
double value = lineEdit->text().toDouble();

model->setData(index, value, Qt::EditRole);
}

void LineEditDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}





void getValue(){
QModelIndex index = m_view->currentIndex();
double size = m_view->model()->data(index, Qt::DisplayRole).toDouble();

//The size is ALWAYS the older value, not the last one
QMessageBox::information(0, "value", QString("%1").arg(size));
}

Any comment is more than welcome

Thanks all

redfric
21st February 2011, 23:02
In createEditor the "connect" is useless.
In setModelData, you are using some "double". Why ? I guess QString would be more appropriate with a QLineEdit, unless you want to type in numbers. If so, don't forget to set a QDoubleValidator in "createEditor" just before the return.


Try this below, it should do the trick :cool: for QStrings



LineEditDelegate::LineEditDelegate(QObject *parent)
: QItemDelegate(parent)
{
}

QWidget *LineEditDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QLineEdit * = new QLineEdit(parent);

return editor;
}

void LineEditDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();

QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
lineEdit->setText(value);
}

void LineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
// lineEdit->returnPressed();
QString value = lineEdit->text();

model->setData(index, value, Qt::EditRole);
}

void LineEditDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}


Be Qt and have fun !