Reputation: 245
I'm writing a program(just for learning purposes, I want to learn C++) in the Qt framework. Is it possible to get how many monitors are connected to my computer and make the program start on different monitor? I want to have an option in the Properties menu where I can tell on which monitor to show the program.
I could not find anything in the Qt developer wiki, maybe you guys can help me with documention on how to do this?
Thanks
Upvotes: 6
Views: 6572
Reputation: 96167
You can get the number of monitors and the total screen size with QDesktopWidget eg.
QDesktopWidget *desktop = QApplication::desktop();
if (desktop->screenCount() == 1) {
// single monitor - use built in
showFullScreen();
} else {
QRect rect = desktop->screenGeometry(1);
move(rect.topLeft());
setWindowState(Qt::WindowFullScreen);
}
Upvotes: 14
Reputation: 11
Edit main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QWindow>
#include <QDesktopWidget>
#include <QDebug>
int main(int argc, char *argv[])
{
int ScreenIDWhereToShowWindow = 1;
QApplication a(argc, argv);
MainWindow w;
QDesktopWidget *desk = new QDesktopWidget();
w.setGeometry(desk->screenGeometry(ScreenIDWhereToShowWindow));
w.showFullScreen();
QApplication::setOverrideCursor(Qt::BlankCursor);
qDebug() << desk->screenGeometry(0);
qDebug() << desk->screenGeometry(1);
qDebug() << desk->screenGeometry(2);
return a.exec();
}
Upvotes: 1
Reputation: 46509
You can use QDesktopWidget to identify how many screens you have attached to your computer and then to retrieve the geometry of each screen:
if (desktopWidget->screenCount() > 1)
{
QRect geom = desktopWidget->screenGeometry(1);
// do something useful with this information
}
You may also want to check to see if it's a virtual desktop, but that may not matter to your users.
Upvotes: 8