使用Qt在多桌面环境中以编程方式确定屏幕几何 [英] Determine programmatically screens geometry in a multi-desktop environment with Qt

查看:370
本文介绍了使用Qt在多桌面环境中以编程方式确定屏幕几何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Qt 4.8编写一个截图应用程序(有点像puush),并且我有多个屏幕设置的麻烦,特别是当主显示器(坐标(0,0))不是最左边的一个。



警告:这不是此问题的重复:正在使用的QDesktopWidget小部件,并计算整个矩形。

  int screens = desktop-> screenCount(); 
QRect wholeDisplayGeometry;
for(int i = 0; i< screens; ++ i){
QRect screenRect = desktop-> screen(i)
wholeDisplayGeometry = wholeDisplayGeometry.united(screenRect); // union
}

现在你可以使用矩形信息拍摄:

  QPixmap shot = QPixmap :: grabWindow(
desktop-> winId(),
wholeDisplayGeometry.x ),
wholeDisplayGeometry.y(),
wholeDisplayGeometry.width(),
wholeDisplayGeometry.height()
);


I am writing a screenshotting app (a bit like puush) using Qt 4.8, and I am having troubles with multiple screens setups, especially when the main monitor (with coordinates (0,0)) is not the leftmost one.

Warning : this is NOT a duplicate of this question : Capture multiple screens desktop image using Qt4, as it solved partially the problem.

The first original code was the following, which worked fine until you had more than one monitor, as only the main one would be captured.

QPixmap shot = QPixmap::grabWindow (QApplication::desktop->winId ());

Result:

Then the thread linked previously headed me towards specifying the full desktop width and height :

QDesktopWidget *desktop = QApplication::desktop ();
QPixmap shot = QPixmap::grabWindow (desktop->winId (), 0, 0, desktop->width(), desktop->height());

But because my second monitor is on the left, its x coordinates are in the negative, resulting in the following screenshot :

The second monitor is 1680px wide, so feeding -1680 to the second argument of grabWindow made a proper screenshot :

So here is the question : how can I determine programmatically this -1680 value to feed to Qt ? This corresponds to the minimal x coordinate allowed on the screen.

Maybe moving the mouse to something like -100000px then grabbing back its coordinates would work, but it seems a bit too "hackish" to me (and might cause issues in applications grabbing the mouse, eg. games). A portable Qt solution would be the best, but OS specific code is fine too, I have not tested my code under X11 or a muli-screen Mac.

解决方案

You can access each screen information inside the QDesktopWidget widget you are using and compute the overall rectangle for the snapshot.

int screens = desktop->screenCount();
QRect wholeDisplayGeometry;
for(int i = 0; i < screens; ++i ){
     QRect screenRect = desktop->screen(i)->geometry();
     wholeDisplayGeometry = wholeDisplayGeometry.united(screenRect); //union
}

Now you can take the shot using the rectangle information :

QPixmap shot = QPixmap::grabWindow (
    desktop->winId (), 
    wholeDisplayGeometry.x(), 
    wholeDisplayGeometry.y(), 
    wholeDisplayGeometry.width(), 
    wholeDisplayGeometry.height()
);

这篇关于使用Qt在多桌面环境中以编程方式确定屏幕几何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆