Results 1 to 2 of 2

Thread: QLineEdit Delegate value update

  1. #1
    Join Date
    Jul 2009
    Location
    Jordan, and UAE
    Posts
    55
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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


    Qt Code:
    1. LineEditDelegate::LineEditDelegate(QObject *parent, Myclass* mc)
    2. : QItemDelegate(parent), m_mc(mc)
    3. {
    4. }
    5.  
    6. QWidget *LineEditDelegate::createEditor(QWidget *parent,
    7. const QStyleOptionViewItem &/* option */,
    8. const QModelIndex &/* index */) const
    9. {
    10. QLineEdit *editor = new QLineEdit(parent);
    11.  
    12. connect(editor, SIGNAL(returnPressed()), m_mc, SLOT(getValue() ) );
    13.  
    14. //connect(editor, SIGNAL(textChanged(QString)), editor, SLOT(setText(QString)) );
    15.  
    16. return editor;
    17. }
    18.  
    19. void LineEditDelegate::setEditorData(QWidget *editor,
    20. const QModelIndex &index) const
    21. {
    22. QString value = index.model()->data(index, Qt::EditRole).toString();
    23.  
    24. QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
    25. lineEdit->setText(value);
    26. }
    27.  
    28. void LineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    29. const QModelIndex &index) const
    30. {
    31. QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
    32. double value = lineEdit->text().toDouble();
    33.  
    34. model->setData(index, value, Qt::EditRole);
    35. }
    36.  
    37. void LineEditDelegate::updateEditorGeometry(QWidget *editor,
    38. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    39. {
    40. editor->setGeometry(option.rect);
    41. }
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. void getValue(){
    2. QModelIndex index = m_view->currentIndex();
    3. double size = m_view->model()->data(index, Qt::DisplayRole).toDouble();
    4.  
    5. //The size is ALWAYS the older value, not the last one
    6. QMessageBox::information(0, "value", QString("%1").arg(size));
    7. }
    To copy to clipboard, switch view to plain text mode 

    Any comment is more than welcome

    Thanks all

  2. #2
    Join Date
    Feb 2011
    Location
    France
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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 for QStrings


    Qt Code:
    1. LineEditDelegate::LineEditDelegate(QObject *parent)
    2. : QItemDelegate(parent)
    3. {
    4. }
    5.  
    6. QWidget *LineEditDelegate::createEditor(QWidget *parent,
    7. const QStyleOptionViewItem &/* option */,
    8. const QModelIndex &/* index */) const
    9. {
    10. QLineEdit * = new QLineEdit(parent);
    11.  
    12. return editor;
    13. }
    14.  
    15. void LineEditDelegate::setEditorData(QWidget *editor,
    16. const QModelIndex &index) const
    17. {
    18. QString value = index.model()->data(index, Qt::EditRole).toString();
    19.  
    20. QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
    21. lineEdit->setText(value);
    22. }
    23.  
    24. void LineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    25. const QModelIndex &index) const
    26. {
    27. QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
    28. // lineEdit->returnPressed();
    29. QString value = lineEdit->text();
    30.  
    31. model->setData(index, value, Qt::EditRole);
    32. }
    33.  
    34. void LineEditDelegate::updateEditorGeometry(QWidget *editor,
    35. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    36. {
    37. editor->setGeometry(option.rect);
    38. }
    To copy to clipboard, switch view to plain text mode 


    Be Qt and have fun !

Similar Threads

  1. Default delegate displays empty QLineEdit?
    By andy.fillebrown in forum Qt Programming
    Replies: 3
    Last Post: 16th April 2009, 14:13
  2. QLineEdit actions in Delegate
    By mclark in forum Qt Programming
    Replies: 1
    Last Post: 15th January 2009, 22:34
  3. Replies: 2
    Last Post: 5th February 2008, 15:58
  4. Delegate but when
    By uygar in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2007, 21:28
  5. Replies: 1
    Last Post: 13th July 2006, 22:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.