Reputation: 2444
I have a Qt-based application running on Windows that displays a QSplashScreen while some initialization occurs, and then I bring up a QMainWindow. The splash screen always displays on monitor 0 regardless of the monitor the QMainWindow uses. For example, if I start the application from a Command Prompt on monitor 0, both the splash screen and main window appear on monitor 0. If I start the application from a Command Prompt on monitor 1, the splash screen appears on monitor 0 and the main window on monitor 1.
To be very clear: I am not trying to control which monitor the main window uses; my only desire is to detect which one it's going to use, and then have the splash screen appear on the same monitor. I know there are other questions about how to control which monitor the window uses, and that's not what I'm trying to understand. It's very obvious from my debug code below that Windows is somehow deciding on which monitor to use, and I'm trying to figure out how to know that without displaying anything. This is also very clearly NOT about the primary monitor. Otherwise, my main window would always appear on the primary monitor, and it doesn't. Also, I do not have any code that is saving/restoring window locations. Windows and/or Qt are positioning the main window and splash screen using their defaults.
I put in some debug code as follows:
QSplashScreen splash (QPixmap (":/splash.png"));
splash.show ();
qDebug () << __LINE__ << "Splash screen monitor: " << app->desktop ()->screenNumber (&splash); // Line 114 in output
app->processEvents ();
// Do a bunch of behind-the-scenes visualization.
aBunchOfStuff ();
// Create and activate main window.
QMainWindow *main_wnd = new QMainWindow ();
qDebug () << __LINE__ << "Main window monitor (created): " << app->desktop ()->screenNumber (main_wnd); // Line 139 in output
main_wnd->show ();
qDebug () << __LINE__ << "Main window monitor (shown): " << app->desktop ()->screenNumber (main_wnd); // Line 141 in output
Output when Command Prompt is on monitor 0:
10/10/16 16:06:53: Debug: 114 Splash screen monitor: 0
10/10/16 16:06:54: Debug: 139 Main window monitor (created): 0
10/10/16 16:06:54: Debug: 141 Main window monitor (shown): 0
Output when Command Prompt is on monitor 1:
10/10/16 16:07:01: Debug: 114 Splash screen monitor: 0
10/10/16 16:07:03: Debug: 139 Main window monitor (created): 0
10/10/16 16:07:03: Debug: 141 Main window monitor (shown): 1
As you can see, between being created and being shown, the main window's monitor number changes from 0 to 1 when the Command Prompt is on monitor 1.
With any of the techniques that I know using QDesktopWidget, it appears that I can't know which monitor the main window will appear on until I've actually shown it. I considered creating a dummy window before showing the splashing screen, but I don't want anything to flicker on screen, and the output between lines 139 and 141 shows that the widget doesn't know it's monitor until it's made visible.
Is there some way, whether using Qt code and or the Windows API, to know what default monitor Windows has assigned to my application?
Upvotes: 2
Views: 914
Reputation: 53
Since windows will show your application on the screen, which contains your cursor, you can get the current screen number with
QApplication::desktop()->screenNumber(QCursor::pos())
To move your splashscreen to this screen, use:
QSplashScreen* splash = new QSplashScreen;
QDesktopWidget* desktop = QApplication::desktop();
const int scrn = desktop->screenNumber(QCursor::pos());
const QPoint currentDesktopsCenter = desktop->availableGeometry(scrn).center();
splash->move(currentDesktopsCenter - splash->rect().center());
splash->show();
Upvotes: 2