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

[OS X] A native dialog run from a menu action causes window activation confusion

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P4: Low
    • None
    • 5.6, 5.12
    • QPA
    • None
    • OS X 10.11.4, at least.
      Qt 5.6.1, but the problem has been with us for a long time.
    • macOS

    Description

      If you put up a native dialog, like QFileDialog::getOpenFileName(), from an action in a contextual menu, window activation is confused. It's easier to use an example to explain...

      Here is a self-contained mini application:

      #include <QApplication>
      #include <QMainWindow>
      #include <QMouseEvent>
      #include <QFileDialog>
      #include <QString>
      #include <QDebug>
      #include <QMenu>
      #include <QAction>
      
      class MainWindow : public QMainWindow
      {
      	public:
      		explicit MainWindow(QWidget *parent = 0);
      		~MainWindow();
      
      	protected:
      		void	mouseReleaseEvent(QMouseEvent * event);
      
      	private:
      };
      
      MainWindow::MainWindow(QWidget *parent) :
      	QMainWindow(parent)
      {
      }
      
      MainWindow::~MainWindow()
      {
      }
      
      void MainWindow::mouseReleaseEvent(QMouseEvent * event)
      {
      	QMenu menu;
      	menu.addAction("Open File...");
      	QAction * selectedAction = menu.exec(event->globalPos());
      
      	if (selectedAction)
      	{
      		QFileDialog::Options options = 0;
      		// options |= QFileDialog::DontUseNativeDialog;
      		QString fullQtPath = QFileDialog::getOpenFileName(NULL, QStringLiteral("Dialog Caption"), QString(), QString(), nullptr, options);
      		qDebug() << fullQtPath;
      		QWidget * activewidget = QApplication::activeWindow();
      		if (activewidget)
      			qDebug() << "Qt thinks that" << activewidget->objectName() << "is active";
      	}
      }
      
      int main(int argc, char *argv[])
      {
      	QApplication a(argc, argv);
      	MainWindow w;
      	w.show();
      
      	return a.exec();
      }
      

      When you run it, a window is created. If you click in the window, a menu is presented. If you choose the only item, "Open File...", QFileDialog::getOpenFileName() is called to put up a native Open File dialog. Now dismiss the dialog (open or cancel, doesn't matter). Now the window appears to be inactive. But if you call QApplication::activeWindow(), it tells you that the window is still active.

      The confusion in activation causes other strange symptoms that I won't go into here, but I think it may be responsible for the Qt Creator New Application wizard going behind the Creator frame when you Browse and choose a location for the application project folder.

      In our real application we have the ability to receive NSNotifications from the native window. That shows that when the dialog goes up, the window receives NSWindowDidResignKeyNotification. If the dialog is put up without the menu action (that is, just in response to the mouseReleaseEvent) then when the dialog is closed the window receives NSWindowDidBecomeKeyNotification. But in the application above, it does not receive that notification, showing that OS X thinks the window is still inactive. And the debug line above shows that Qt thinks the window is active.

      Attachments

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

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            johnweeks John Weeks
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes