Results 1 to 10 of 10

Thread: Model/View framework, delegate: don't close editor while it contains invalid value

  1. #1
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Model/View framework, delegate: don't close editor while it contains invalid value

    Dear friends,

    Is it possible to obtain the next behavior?

    - user starts editing view's cell;
    - when he/she finishes, delegate (or who?) checks is the new value is correct;
    -- if YES: save data to model;
    -- if NO: show message box "Data is invalid" and leave editor and the focus in it so user could fix the value at the same moment.

    I tried to use event filter installed on the editor (created by delegate::createEditor()) by handling FocusOut events, but when user clicks the other cell in the view the current editor gets closed anyway and one creates new editor for the clicked cell.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Model/View framework, delegate: don't close editor while it contains invalid valu

    Yes, it is possible.

    You need have sub-class of QItemDelegate, and your data validation need to go into implementation of QItemDelegate::setModelData(), where the data has to be written to the model, you can validate and throw Model dialogs from there.

  3. #3
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View framework, delegate: don't close editor while it contains invalid valu

    Quote Originally Posted by Santosh Reddy View Post
    Yes, it is possible.

    You need have sub-class of QItemDelegate, and your data validation need to go into implementation of QItemDelegate::setModelData(), where the data has to be written to the model, you can validate and throw Model dialogs from there.
    But how can I prevent current editor from being closed? I want user to continue editing after he closes the "error" dialog.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Model/View framework, delegate: don't close editor while it contains invalid valu

    Try QAbstractItemDelegate::editorEvent() to handle event from view

    I tried to use event filter installed on the editor (created by delegate::createEditor()) by handling FocusOut events, but when user clicks the other cell in the view the current editor gets closed anyway and one creates new editor for the clicked cell.
    I think you should handle QEvent::Close for the editor widget.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model/View framework, delegate: don't close editor while it contains invalid valu

    Quote Originally Posted by stillwaiting View Post
    But how can I prevent current editor from being closed? I want user to continue editing after he closes the "error" dialog.
    So reopen the editor instead of trying to make it remain open.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Model/View framework, delegate: don't close editor while it contains invalid valu

    Quote Originally Posted by wysota View Post
    So reopen the editor instead of trying to make it remain open.
    wysota,

    Do you think it is possible to start editor from a delegate? I guess only view has a control to start edting

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model/View framework, delegate: don't close editor while it contains invalid valu

    So pass the pointer to the view to the delegate. Besides, the delegate has a pointer to the view but it is hidden in the docs because it is easy to abuse it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: Model/View framework, delegate: don't close editor while it contains invalid valu

    Quote Originally Posted by Santosh Reddy View Post
    Try QAbstractItemDelegate::editorEvent() to handle event from view



    I think you should handle QEvent::Close for the editor widget.
    Thank you for you suggestions but... no success when I clicks the other cell one closes the current editor and shows the new one.


    I implemented the workaround by installing the event filter on "QApplication::instance()": when value is not valid then the filter blocks mouse and keyboard events which can change the focus (MouseClick, KeyUp, KeyDown, KeyTab etc) and shows error dialog.

    Looks like ugly hack but that's the best idea I had.

  9. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Model/View framework, delegate: don't close editor while it contains invalid valu

    The documentation says

    void QAbstractItemDelegate::closeEditor ( QWidget * editor, QAbstractItemDelegate::EndEditHint hint = NoHint ) [signal]
    This signal is emitted when the user has finished editing an item using the specified editor.
    The hint provides a way for the delegate to influence how the model and view behave after editing is completed. It indicates to these components what action should be performed next to provide a comfortable editing experience for the user. For example, if EditNextItem is specified, the view should use a delegate to open an editor on the next item in the model.
    which means that signal to close to editor is emitted by the QAbstractItemDelegate (base class), we don't have any control over this, basically we cannot stop editor being closed from delegate, unless we block all signals from the delegate which is not practical

    another way (other than event filtering) would be to send a signal to the QAbstractItemView::edit () slot of the view to restart the editor (after validating the data)

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model/View framework, delegate: don't close editor while it contains invalid valu

    Quote Originally Posted by Santosh Reddy View Post
    another way (other than event filtering) would be to send a signal to the QAbstractItemView::edit () slot of the view to restart the editor (after validating the data)
    That's exactly what I meant.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    oberlus (21st September 2011)

Similar Threads

  1. Replies: 9
    Last Post: 8th March 2011, 08:35
  2. Model-View-Delegate
    By hotdox in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2010, 13:20
  3. Data structure reference in Model/View Framework
    By jimc1200 in forum Qt Programming
    Replies: 4
    Last Post: 14th September 2009, 20:23
  4. Model/View framework: streaming data in a QTableView
    By yannickt in forum Qt Programming
    Replies: 6
    Last Post: 24th October 2008, 00:06
  5. Model View - Delegate - setIndexWidget
    By starcontrol in forum Qt Programming
    Replies: 16
    Last Post: 2nd April 2008, 14:30

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.