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
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);
}
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With