Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-45893

Some QEvents aren't delivered as expected to QWidgets embedded in a QWidgetAction

    XMLWordPrintable

Details

    • eadd7e9cfb4e0802aa9da63badc41c39fec219bb

    Description

      I believe the issue may be more general than I've taken the time to demonstrate, but I'm seeing that if I place a QTreeView inside a QWidgetAction that is then placed inside a submenu of a QMenu that's presented as a context menu (yes, seems terribly convoluted, but I'm actually trying to do this in a real-world app!), then the QTreeView doesn't receive Leave events as the user moves the mouse over the QTreeView.

      Code below demonstrates this. You have to right-click on the QLabel to spawn the menu, and then navigate to the "Sub Menu With Widget" submenu to get the QTreeView to appear:

      
      #include <QApplication>
      #include <QLabel>
      #include <QMenu>
      #include <QWidgetAction>
      #include <QTreeView>
      
      int main( int argc, char *argv[] )
      {
      	QApplication app(argc, argv);
      
      	QLabel* label = new QLabel("Right Click On Me");
      	label->setContextMenuPolicy(Qt::CustomContextMenu);
      	QObject::connect(label, &QWidget::customContextMenuRequested, [label](const QPoint & pos)
      	{
      		QMenu menu;
      
      		menu.addAction(new QAction("Some action", &menu));
      
      		QMenu* subMenu = menu.addMenu("Sub Menu With Widget");
      
      		class CustomTreeView : public QTreeView
      		{
      		public:
      			CustomTreeView() : QTreeView() {}
      
      		protected:
      			virtual void leaveEvent(QEvent * event)
      			{
      				QTreeView::leaveEvent(event); // !! moving the mouse over the CustomTreeView and then out of the CustomTreeView doesn't trigger this event!
      			}
      		};
      
      		QWidgetAction* widgetAction = new QWidgetAction(subMenu);
      		widgetAction->setDefaultWidget(new CustomTreeView);
      		subMenu->addAction(widgetAction);
      
      		menu.exec(label->mapToGlobal(pos));
      	});
      
      	label->show();
      
      	app.exec();
      
      	return 0;
      }
      

      Interestingly, this issue doesn't occur if QWidgetAction containing the QTreeView is placed in the top-level menu that's executed, instead of in the submenu.

      This is an issue for a few reasons, one being that some internal logic of QTreeView (e.g. the code which determines which is the 'hover' item) expects to always receive a Leave event when the mouse leaves the widget.

      In general, this problem is preventing me from being able to highlight the items on hover in such an embedded QTreeView. I'm finding that the HoverLeave event isn't being sent as expected in a similar way.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            zaps166 Błażej Szczygieł
            jacob.enget Jacob Enget
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes