Results 1 to 3 of 3

Thread: Focus back to an item of QTreeWidget

  1. #1
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Focus back to an item of QTreeWidget

    Hi!

    I'm using the property browser from Qt Solution, it uses the model/view architecture, so it construct the widget when the editing is necessary and destroy it after editing.

    In my software when the editing is finished (when the control loses the focus by a mouse click or keyboard) I check the modified value to know if it is valid. When the value is not valid a message box opens with a warning, when the message box is closed I need to set the focus back for editing. The problem is that now the control was already or will be destroyed and set the focus to the widget will not work.

    The property browser uses a QTreeWidget to handle with all that. Does anybody know how to set the focus and opens it to edit of an QTreeWidget's item?
    How can I do that?

    Thank you in advance.

  2. #2
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus back to an item of QTreeWidget

    I can set the focus manually with QTreeWidget::editItem(index,1) to create the editor and QWidget::setFocus() in the created editor. But when the control is editing and loses the focus and I try this way, the editor is destroyed again by some post event.

    Any ideas?

  3. #3
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus back to an item of QTreeWidget

    Ok, I think nobody is interested in this topic but I will post my progress here.

    I think my solution is not the best, I had to stop the propagation of the events in order to avoid the destruction of the editor.
    Was necessary to analyse 3 types of events: keyboard, mouse click in the property browser and mouse click outside the property browser.

    The events of the keyboard should be handle in the editor delegate, since was installed an event filter in the editor widget:

    Qt Code:
    1. bool QtPropertyEditorDelegate::eventFilter(QObject *object, QEvent *event)
    2. {
    3. ...
    4.  
    5. if (event->type() == QEvent::KeyPress)
    6. {
    7. QKeyEvent *e = static_cast<QKeyEvent *>(event);
    8. if ( e->key() == Qt::Key_Tab ||
    9. e->key() == Qt::Key_Return ||
    10. e->key() == Qt::Key_Enter )
    11. {
    12. // make the necessary validation
    13. if ( isNotValid )
    14. {
    15. event->accept();
    16. return true;
    17. }
    18. }
    19.  
    20. ...
    21. }
    To copy to clipboard, switch view to plain text mode 

    Is important to stop the event propagation when the value is invalid.

    For the mouse click outside the property browser is necessary to add in the eventFilter of the editor delegate the same check but now for the focus out event. The reason of the focus out in this case is Qt::OtherFocusReason, that is done by the popup dialog:

    Qt Code:
    1. if (event->type() == QEvent::FocusOut)
    2. {
    3. QFocusEvent *fe = static_cast<QFocusEvent *>(event);
    4.  
    5. if ( fe->reason() == Qt::OtherFocusReason )
    6. {
    7. // make the necessary validation
    8. if ( isNotValid )
    9. {
    10. event->accept();
    11. return true;
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    In the last case, the click in another part of the property browser, the event is catch by the QtPropertyEditorView:

    Qt Code:
    1. void QtPropertyEditorView::mousePressEvent(QMouseEvent *event)
    2. {
    3. ...
    4.  
    5. if ( isNotValid )
    6. {
    7. QModelIndex index = currentIndex();
    8. edit(index);
    9. return;
    10. }
    11.  
    12. ...
    13. }
    To copy to clipboard, switch view to plain text mode 

    The reason to interrupt the event is to avoid that the event arise in the QItemDelegate::event, so that the editor is not destroyed. When the message box popup, the editor loses the focus but is still there, then when the message box is closed the focus goes to the right place.

Similar Threads

  1. QListWidget back to previous item in currentItemChanged
    By laszlo.gosztola in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2012, 15:56
  2. Replies: 7
    Last Post: 23rd April 2011, 03:29
  3. Replies: 4
    Last Post: 8th April 2011, 21:59
  4. Replies: 1
    Last Post: 18th November 2008, 07:57
  5. Replies: 5
    Last Post: 5th August 2006, 00:44

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.