Unsolved QItemDelegate with multiple QWidgets inside - Focus problem
-
Hello,
I have a QStyledItemDelegate with a custom pain event that holds several other widgets. Now when the delegate is clicked the focus jumps to the delegate. However it should be at the underlying widget that was clicked on.
If there is only one widget I can achieve the desired behaviour by delegateWidget->setFocusProxy(the_editor_widget);
However this naturally only works for one widget. Is there some method to "forward" the click to the underlying widget? -
Are you looking for event filters ?
-
If eventFilters are the solution, then maybe.
But I do not know how exactly to solve it with event filters.The
QTreeView::mousePressEvent()
triggersQStyledItemDelegate::createEditor(); and QStyledItemDelegate::setEditorData();
.
However, now the click needs to kind of be repeated on the created editor Widget. -
Hi,
One possibility could be to use QApplication::widgetAt in your editor showEvent and check there which widget is under the cursor to give it the focus.
-
@SGaist: Thanks for the hint, however
qApp->widgetAt(QCursor::pos());
returns the viewport of the TreeView (qt_scollarea_viewport) instead of the widget inside the delegate. -
I had similar problem and went through couple of different solutions. My case was specific in the sense that I only wanted the edit to start via mouse press, not other methods (i.e. enter or tab). In that specific case I ended with the following:
Blocked all edit triggers by setting editTriggers to
NoEditTriggers
.
Implemented editorEvent() in the delegate to recognize a mouse press and callingedit()
manually.
In the created widget I implementedshowEvent()
. In it I check if it's the first show event or not. If it's the first I synthesize a fake mouse release event followed by a mouse press event and send them to the app via QApplication::sendEvent(). Some attention was needed to make sure the fake events are with the same mouse buttons and other event options, otherwise you can easily confuse the internals of the view.This way Qt does all the work to figure out which widget should get the focus and mouse press, how to route it through the item view etc. You can probably extend this to other trigger methods. You would just need to tell the widget somehow about which trigger method was used and act accordingly in the
showEvent()
e.g. set focus on the "first" widget when it's via tab key and so forth.