Results 1 to 5 of 5

Thread: QLineEdit with mouse wheel: setModelData is called too late after commitData

  1. #1
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default QLineEdit with mouse wheel: setModelData is called too late after commitData

    Hi,

    In a tree view I would like a QLineEdit where I can change the value with the mouse wheel.
    The following idea works fine when I slowly scroll the wheel. But when I faster scroll the wheel sometimes I am missing a call to setModelData. This function is then only called when I click somewhere else in the tree view.

    Thank you!

    Qt Code:
    1. QLineMouseEdit::QLineMouseEdit(QWidget *aParent): QLineEdit(aParent) {
    2. setFocusPolicy(Qt::WheelFocus);
    3. }
    4.  
    5. void QLineMouseEdit::wheelEvent(QWheelEvent *aEvent) {
    6. double LinearChange = 1;
    7. if (LinearChange!=0) {
    8. double Delta = aEvent->delta();
    9. double OldValue = text().toDouble();
    10. double NewValue = OldValue + Delta/120*LinearChange;
    11. setText(QString::number(NewValue));
    12. qDebug() << "";
    13. qDebug() << "wheelEvent emit changed" << NewValue; // ok!!
    14. emit changed();
    15. return;
    16. }
    17. }
    18.  
    19. QWidget *CModelTreeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    20. QLineMouseEdit *Editor = new QLineMouseEdit(parent);
    21. connect(Editor, SIGNAL(changed()), this, SLOT(changed()));
    22. return Editor;
    23. }
    24.  
    25. void CModelTreeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
    26. qDebug() << "CModelTreeDelegate::setModelData"; // ??? sometimes too late ???
    27. QItemDelegate::setModelData(editor, model, index);
    28. }
    29.  
    30. void CModelTreeDelegate::changed() {
    31. QLineMouseEdit *Editor = qobject_cast<QLineMouseEdit *>(sender());
    32. if (Editor) {
    33. qDebug() << "CComboDelegate::changed emit commitData"; // ok!!
    34. emit commitData(Editor);
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QLineEdit with mouse wheel: setModelData is called too late after commitData

    It seems that the data you're dealing with is a decimal number. Why don't you use a QDoubleSpinBox instead of a QLineEdit.
    If I properly understood what you want to achieve using a QDoubleSpinBox will solve your problem because it supports changing the value using the mouse wheel out of the box. To achieve the step to be done like you need it to be done I suggest overwriting void QAbstractSpinBox::stepBy(int steps).

  3. The following user says thank you to Infinity for this useful post:

    ctgrund (12th February 2014)

  4. #3
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit with mouse wheel: setModelData is called too late after commitData

    @Infinity: your approach does really fit my situation much more!
    But: still got update problems when I scroll the mouse too quickly.

    Thanks anyway!

  5. #4
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QLineEdit with mouse wheel: setModelData is called too late after commitData

    still got update problems when I scroll the mouse too quickly
    Maybe we are able to help if you post your code.

  6. The following user says thank you to Infinity for this useful post:

    ctgrund (12th February 2014)

  7. #5
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit with mouse wheel: setModelData is called too late after commitData

    @Infinity: I definitly will post some code. I 'only' have to strip down the code to some short example.
    Thanks for your help!

Similar Threads

  1. QPrintPreviewWidget mouse wheel events
    By ChrisW67 in forum Qt Programming
    Replies: 3
    Last Post: 26th October 2013, 06:32
  2. Replies: 0
    Last Post: 6th March 2012, 10:56
  3. Can usb mouse wheel work in Qt/E ?
    By earth in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 26th August 2011, 11:00
  4. Scrolling on Mouse Wheel
    By penny in forum Qt Programming
    Replies: 4
    Last Post: 7th April 2011, 07:30
  5. commitData doesn't seem to be called...
    By tone in forum Qt Programming
    Replies: 7
    Last Post: 10th September 2008, 14:42

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.