PDA

View Full Version : Focus back to an item of QTreeWidget



San_Cou
14th November 2012, 18:27
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.

San_Cou
16th November 2012, 17:27
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?

San_Cou
28th November 2012, 11:33
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:



bool QtPropertyEditorDelegate::eventFilter(QObject *object, QEvent *event)
{
...

if (event->type() == QEvent::KeyPress)
{
QKeyEvent *e = static_cast<QKeyEvent *>(event);
if ( e->key() == Qt::Key_Tab ||
e->key() == Qt::Key_Return ||
e->key() == Qt::Key_Enter )
{
// make the necessary validation
if ( isNotValid )
{
event->accept();
return true;
}
}

...
}


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:



if (event->type() == QEvent::FocusOut)
{
QFocusEvent *fe = static_cast<QFocusEvent *>(event);

if ( fe->reason() == Qt::OtherFocusReason )
{
// make the necessary validation
if ( isNotValid )
{
event->accept();
return true;
}
}
}


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



void QtPropertyEditorView::mousePressEvent(QMouseEvent *event)
{
...

if ( isNotValid )
{
QModelIndex index = currentIndex();
edit(index);
return;
}

...
}


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.