Qt multi screen Support

原文地址::https://www.e-consystems.com/blog/linux-android/?p=562


Published on January 12, 2015

Introduction:
This article discusses how to use a Qt application in a multi-head system and to display windows across the monitors.

What is QdesktopWidget?
The QDesktopWidget class provides access to screen information on multi-head systems. QDesktopWidget class provides information about the user’s desktop, such as its total size, number of screens, the geometry of each screen, and whether they are configured as separate desktops or a single virtual desktop.
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 to start a window in the secondary screen?
As a first step, it is required to get the root window using QApplication::desktop(). With the instance returned by QApplication::desktop(), we can get the number of screens available using  screenCount() and the geometry of each screen using screenGeometry().
Once the above steps are performed, we can move the window to the secondary screen by getting the geometry of the secondary screen and move using the move() function.

How to save the positions of the window ?

QRect rect = widget->screenGeometry(1);

We can use screenGeometry()for getting the geometry of the screen whose positions are to be saved and restored.
The screen geometry can be saved and restored from a simple ini file using QSettings.
Sample Application:

#include <…>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w1;
MainWindow w2;
QDesktopWidget *widget = QApplication::desktop();
qDebug() << “Number of screens:” << widget->screenCount();
for (int i = 0; i < widget->screenCount(); i++) {
qDebug() << “Geometry:” << widget->screenGeometry(i);
}
// Get geometry of Secondary screen
QRect rect = widget->screenGeometry(1);
qDebug() << “Move to:” << rect.width() << “:” << rect.y();
w1.show();
//Move w2 to Secondary Screen
w2.move(rect.width(), rect.y());
w2.show();
return a.exec();
}

References

1. http://doc.qt.io/qt-5/qdesktopwidget.html

For any technical support or queries feel free to write to us at sales@e-consystems.com


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值