Details
-
Bug
-
Resolution: Unresolved
-
P2: Important
-
None
-
5.15.1
-
None
Description
Repro Steps:
- Build and launch the app below on a PC with two displays
- Close the app's main window
- Disconnect a display
- Restore the app from the system tray icon (via double click)
- 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
- relates to
-
QTBUG-108386 Windows: QScreen::devicePixelRatio sometimes inaccurate results
-
- Closed
-