Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start program on second monitor (Qt framework)

Tags:

qt

qt4

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

like image 612
Dan Mooray Avatar asked Oct 05 '10 21:10

Dan Mooray


3 Answers

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);       
}
like image 172
Martin Beckett Avatar answered Sep 22 '22 02:09

Martin Beckett


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.

like image 30
Kaleb Pederson Avatar answered Sep 23 '22 02:09

Kaleb Pederson


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();
}
like image 1
Tomáš Kello Avatar answered Sep 19 '22 02:09

Tomáš Kello