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
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