QLineEdit Delegate value update
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
Code:
LineEditDelegate
::LineEditDelegate(QObject *parent, Myclass
* mc
){
}
{
connect(editor, SIGNAL(returnPressed()), m_mc, SLOT(getValue() ) );
//connect(editor, SIGNAL(textChanged(QString)), editor, SLOT(setText(QString)) );
return editor;
}
void LineEditDelegate
::setEditorData(QWidget *editor,
{
QString value
= index.
model()->data
(index, Qt
::EditRole).
toString();
QLineEdit *lineEdit
= static_cast<QLineEdit
*>
(editor
);
lineEdit->setText(value);
}
{
QLineEdit *lineEdit
= static_cast<QLineEdit
*>
(editor
);
double value = lineEdit->text().toDouble();
model->setData(index, value, Qt::EditRole);
}
void LineEditDelegate
::updateEditorGeometry(QWidget *editor,
{
editor->setGeometry(option.rect);
}
Code:
void getValue(){
double size = m_view->model()->data(index, Qt::DisplayRole).toDouble();
//The size is ALWAYS the older value, not the last one
}
Any comment is more than welcome
Thanks all
Re: QLineEdit Delegate value update
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
Code:
LineEditDelegate
::LineEditDelegate(QObject *parent
){
}
{
return editor;
}
void LineEditDelegate
::setEditorData(QWidget *editor,
{
QString value
= index.
model()->data
(index, Qt
::EditRole).
toString();
QLineEdit *lineEdit
= static_cast<QLineEdit
*>
(editor
);
lineEdit->setText(value);
}
{
QLineEdit *lineEdit
= static_cast<QLineEdit
*>
(editor
);
// lineEdit->returnPressed();
model->setData(index, value, Qt::EditRole);
}
void LineEditDelegate
::updateEditorGeometry(QWidget *editor,
{
editor->setGeometry(option.rect);
}
Be Qt and have fun !