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

Windows: QGuiApplication::screens sometimes reports inaccurate results

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.15.1
    • QPA: Windows
    • None
    • Windows

    Description

      Repro Steps:

      1. Build and launch the app below on a PC with two displays
      2. Close the app's main window
      3. Disconnect a display
      4. Restore the app from the system tray icon (via double click)
      5. Observe

      Expected Result:

      • The screen count is "1"

      Actual Result:

      • The screen count remains at "2"

      Additional Info:

      • QGuiApplication::screens() is pulling from a cached screen list (QWindowsScreenManager::m_screens)
      • Qt seems to be relying on the WM_DISPLAYCHANGE event to update this list.  WM_DISPLAYCHANGE is sent to top level windows so the list is not updated properly if no Qt windows are open when the display is disconnected
      • If you skip step 2 (closing the window), the screen count updates as expected

      Code sample:

      #include <QApplication>
      #include <QLabel>
      #include <QScreen>
      #include <QStyle>
      #include <QSystemTrayIcon>
      
      int main(int argc, char *argv[])
      {
      	QApplication app(argc, argv);
      	app.setApplicationName("Qt screen count bug");
      	app.setQuitOnLastWindowClosed(false);
      
      	QLabel window;
      	window.resize({ 300, 100 });
      	window.show();
      
      	auto updateText = [&window]()
      	{
      		window.setText(QString("Screen count: %1").arg(QGuiApplication::screens().count()));
      	};
      	updateText();
      
      	QGuiApplication* guiApp = static_cast<QGuiApplication*>(&app);
      	QObject::connect(guiApp, &QGuiApplication::screenAdded, updateText);
      	QObject::connect(guiApp, &QGuiApplication::screenRemoved, updateText);
      
      	QSystemTrayIcon icon;
      	icon.setIcon(QApplication::style()->standardIcon(QStyle::SP_ComputerIcon));
      	icon.show();
      
      	// Not using a context menu here because opening the context menu creates
      	// a long-lived HWND that will handle the WM_DISPLAYCHANGE event. This
      	// would cause the repro steps to work only once per session.
      	QObject::connect(&icon, &QSystemTrayIcon::activated, [&](auto reason)
      	{
      		switch (reason)
      		{
      		case QSystemTrayIcon::DoubleClick:
      			updateText();
      			window.show();
      			break;
      
      		case QSystemTrayIcon::MiddleClick:
      		case QSystemTrayIcon::Context:
      			app.exit();
      			break;
      
      		default:
      			break;
      		}
      	});
      
      	return app.exec();
      }
      

       

      Attachments

        Issue Links

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

          Activity

            People

              owolff Oliver Wolff
              mloftus Mitchell Loftus
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes