How do I make my app appear on the secondary monitor?



  • I am able to successfully override the beginning size and position of the screen, but not which monitor it appears on. Using Visual Studio, Windows 10, Qt 5.13.

    Here's what I have tried (note screen is set to '1', the secondary monitor)

    In the following case, 'qwin' is NULL, can't invoke setScreen()

    MainWindow::MainWindow()	// constructor of main window; 'MainWindow' is a QMainWindow which inherits from QWidget
    {	// 'theApp' is QApplication *
    	QWindow *qwin = windowHandle();
    	if(qwin)	// 'qwin' is NULL, even though the parent is a QMainWindow which inherits from QWidget
    	{	QList<QScreen *> qscreens = theApp->screens();
    		if(screen < qscreens.size())
    			qwin->setScreen(qscreens[screen]);
    	}
    }
    

    In the following case, 'qwin' is NULL, can't invoke setScreen()

    MainWindow::MainWindow()
    {	QWidget *topWidg = window();
    	if(topWidg)		// not NULL
    	{	QWindow *qwin = topWidg->windowHandle();
    		if(qwin)	// NULL
    		{	QList<QScreen *> qscreens = theApp->screens();
    			if(screen < qscreens.size())
    				qwin->setScreen(qscreens[screen]);
    		}
    	}
    }
    

    In the following case, 'qwin' is NOT NULL, but setScreen() does not take effect,
    the app stays on primary screen (0) instead of appearing on secondary screen (1)

    MainWindow::MainWindow()
    {	QDesktopWidget qdskWidg = theApp->desktop(); // 'theApp' is QApplication 
    	if(qdskWidg)	// not NULL
    	{	QWindow *qwin = qdskWidg->windowHandle();
    		if(qwin)	// not NULL
    		{	QList<QScreen *> qscreens = theApp->screens();
    			if(screen < qscreens.size())
    				qwin->setScreen(qscreens[screen]);	// stays on primary (0) even though 'screen' is 1
    		}
    	}
    }
    

    Does anyone have a simple "Hello World" example that explicitly positions itself on the second monitor?

    Thanks.


  • Lifetime Qt Champion

    Hi,

    From the top of my head, I would get the center of the screen geometry and apply that to the widget.



  • @SGaist You mean like this:

    QDesktopWidget *desktopWidget = qApp->desktop();
    QRect secondScreenGeometry = desktopWidget->screenGeometry(1);
    myWidget->move(secondScreenGeometry.center());
    

    Does not work.



  • Hi, try:

    myWidget->setGeometry(secondScreenGeometry);