Multiple Screens with Qt

Multiple Screens with Qt

Your mistake is wrong geometry. In these 2 lines of code, you place both windows on same position:

view1.setGeometry(0,0,200,200);
view2.setGeometry(0,0,200,200);

Instead of this, you can set the position (not sure if you need size also):

view1.setGeometry(screen1->geometry().x(),screen1->geometry().y(),200,200);
view2.setGeometry(screen2->geometry().x(),screen2->geometry().y(),200,200);

To change the position instead of changing both the position and the size, you can use the function move.

P.S. There may be some small typos as I wrote this code by memory, but the main idea should be clear for you.

Multi Screen/Window QT QML C++ application

Use the Quick Controls 2 StackView control:

https://doc.qt.io/qt-5/qml-qtquick-controls2-stackview.html

This control maintains a stack of QML views where the top one is always visible and you can pop and push views with animation.

multiple monitor application development with Qt and QtCreator

I think QDesktopWidget has all features you need.

From Qt Documentation:

Widgets provided by Qt use this class to place tooltips, menus and dialog boxes on the correct screen for their parent or application widgets. Applications can use this class to obtain information that can be used to save window positions, or to place child widgets and dialogs on one particular screen.

How does Qt enumerate screens?

As far as I've been able to see in different systems, EnumDisplayMonitors returns monitors in the order defined in the Display Settings, while QGuiApplication::screens always shows primary screen at the first position (actually, QGuiApplication::primaryScreen simply do that: return the first element).

Looking at the source code, in Windows Qt also uses the EnumDisplayMonitors function but basically moves the primary screen to the first position (it actually inserts in the first position the primary screen, while inserting at the end of the list any other monitor).

So, the primary screen will be at first position, screens with an index lower than primary screen's will be shifted one position, while the rest will match the index.


As a side note, taken from the comments of the code, if the primary screen is changed during the execution of the application, Qt is not able to report the change.

Note that the side effect of this policy is that there is no way to change primary screen reported by Qt, unless we want to delete all existing screens and add them again whenever primary screen changes.

Application window on Multi monitor screen and centre placement

Maybe try something like this:

void MainWindow::CenterToScreen(QWidget* widget) {
if (!widget)
return;
QDesktopWidget* m = QApplication::desktop();
QRect desk_rect = m->screenGeometry(m->screenNumber(QCursor::pos()));
int desk_x = desk_rect.width();
int desk_y = desk_rect.height();
int x = widget->width();
int y = widget->height();
widget->move(desk_x / 2 - x / 2 + desk_rect.left(), desk_y / 2 - y / 2 + desk_rect.top());
}

and usage:

CenterToScreen(this);  // or CenterToScreen(mainWin);

Screenshot capture of dual monitor in Qt 5

Naturally, QGuiApplication::primaryScreen() will give you a single screen.

You could use QList<QScreen *> QGuiApplication::screens() to get all screens associated with the application, take screenshots for all of them, then create another blank image, size it according to how you want to compose the screens, and manually compose into a final image using QPainter.

QPixmap grabScreens() {
auto screens = QGuiApplication::screens();
QList<QPixmap> scrs;
int w = 0, h = 0, p = 0;
foreach (auto scr, screens) {
QPixmap pix = scr->grabWindow(0);
w += pix.width();
if (h < pix.height()) h = pix.height();
scrs << pix;
}
QPixmap final(w, h);
QPainter painter(&final);
final.fill(Qt::black);
foreach (auto scr, scrs) {
painter.drawPixmap(QPoint(p, 0), scr);
p += scr.width();
}
return final;
}


Related Topics



Leave a reply



Submit