设置QMainWindow在屏幕的中心 [英] Setting QMainWindow at the center of screen

查看:756
本文介绍了设置QMainWindow在屏幕的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 QMainWindow 包含一个 QGraphicsView ,它应该有最小的宽度和高度。因此,我在 QMainWindow 构造函数中使用了以下代码:

My QMainWindow contains a QGraphicsView, which should've minimum width and height. So, I've used the following code in the QMainWindow constructor:

ui.graphicsView->setMinimumHeight(VIEWWIDTH);
ui.graphicsView->setMinimumWidth(VIEWWIDTH);

然后我使用下面的代码设置 QMainWindow

Then I used following code to set QMainWindow at the center of the screen:

QRect available_geom = QDesktopWidget().availableGeometry();
QRect current_geom = frameGeometry();
setGeometry(available_geom.width() / 2 - current_geom.width() / 2,
            available_geom.height() / 2 - current_geom.height() / 2,
            current_geom.width(),
            current_geom.height());

但它未设置在屏幕中央。如果我省略了 setMinimumHeight() setMinimumWidth() QGraphicsView ,则主窗口设置在屏幕中心。如何克服这个问题?我正在使用Qt 4.5.2。

But it is not set at the center of the screen. If I omit setMinimumHeight() and setMinimumWidth() from QGraphicsView, then the main window is set at the center of the screen. How to overcome this problem? I'm using Qt 4.5.2.

谢谢。

推荐答案

你遇到的问题是,Qt会延迟许多计算,只要它可以。当设置图形视图的最小宽度和高度时,它会在图形视图所在的窗口需要重新布局的地方设置一个标志。然而,它不会这样做,直到它需要几个毫秒之前它实际上显示在屏幕上。因此,当您在主窗口中调用 rect()时,您将获得旧的矩形,而不是新的矩形。

The problem you are encountering is that Qt will delay many calculations as long as it can. When you set the minimum width and height of your graphics view, it sets a flag somewhere that the window the graphics view is in needs re-layed out. However, it won't do that until it has to... a few milliseconds before it is actually shown on screen. So, when you call rect() on your main window, you are getting the old rectangle, and not the new one.

我最好的建议是在主窗口中扩展大小更改事件,并调整该事件中的定位。您可能还必须在窗口实际显示后标记,以便在用户在显示窗口后重新调整窗口大小。

My best recommendation is to extend the size change event in your main window, and adjust the positioning in that event. You may also have to flag when the window has actually been shown, so that you don't reposition the window if the user resizes it after it has been shown.

,您可以尝试通过扩展show event函数并在那里重新定位窗口。这可能发生在用户实际看到窗口之前,但偶尔会闪烁。

Alternately, you could try repositioning the window by extending the show event function and doing it there. It would probably happen before the user actually sees the window, but might flicker on occasion.

这篇关于设置QMainWindow在屏幕的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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