如何在 Qt 中存储会话之间的窗口大小?
我在 Qt 应用程序中有一个 QMainWindow。 当我关闭它时,我希望它存储其当前的恢复大小(窗口未最大化时的大小)。 当我在恢复模式(即未最大化)下关闭窗口时,此方法效果很好。 但是,如果我在最大化窗口时关闭窗口,那么下次我启动应用程序并恢复应用程序(因为它以最大化模式启动)时,它不会记住应该恢复到的大小。 有没有办法做到这一点?
I have a QMainWindow in a Qt application. When I close it I want it to store its current restore size (the size of the window when it is not maximized). This works well when I close the window in restore mode (that is, not maximized). But if I close the window if it is maximized, then next time i start the application and restore the application (because it starts in maximized mode), then it does not remember the size it should restore to. Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也遇到过这个问题。
你可以做什么:除了窗口的大小之外,还保存它是否最大化(
QWidget::isMaximized()
)。然后,下次启动应用程序时,首先设置大小 (
QWidget::resize()
),然后在适当的情况下将其最大化 (QWidget::showMaximized()
)。 恢复后,它应该恢复到正确的大小。I've encountered this problem as well.
What you can do: in addition to the window's size, save whether it's maximized or not (
QWidget::isMaximized()
).Then next time you start the application, first set the size (
QWidget::resize()
) and then maximize it if appropriate (QWidget::showMaximized()
). When it's restored, it should return to the correct size.图像位于 http://qt-project.org/doc/qt- 4.8/application-windows.html 显示,
geometry.x()
和geometry.y()
不等于x()< /code> 和
y()
,与pos()
相同。就我而言,我使用:
并通过以下方式成功恢复它们:
The image at http://qt-project.org/doc/qt-4.8/application-windows.html shows, that
geometry.x()
andgeometry.y()
are not equal tox()
andy()
, which are the same aspos()
.In my case, I use:
and restore these successfully with:
使用 QWidget::saveGeometry 功能将当前设置写入注册表。(使用 QSettings 访问注册表)。 然后在启动时使用restoreGeometry()返回到之前的状态。
Use the QWidget::saveGeometry feature to write the current settings into the registry.(The registry is accessed using QSettings). Then use restoreGeometry() upon startup to return to the previous state.
我发现在 Fedora 14 上需要结合前面所有的答案。小心不要在窗口最大化时保存大小和位置!
在 main() 中,在第一次显示窗口之前读取位置设置......
并且这些事件处理程序根据需要更新设置。 (这会导致在移动和调整大小期间每次鼠标移动都会写入设置文件,这并不理想。)
不过,位置的垂直分量不太正确,它似乎忽略了窗口标题栏的高度。 ..如果有人知道如何处理,请告诉我:)
I found that a combination of all the previous answers here was necessary on Fedora 14. Be careful not to save the size and position when the window is maximized!
In main(), the position settings are read before showing the window the first time...
...and these event handlers update the settings as necessary. (This causes a writes to the settings file for every mouse movement during the move and resize which is not ideal.)
Still, the vertical component of the position is not quite right, it seems to be ignoring the height of the window title bar... if anyone knows how to deal with that let me know :)