Qt:如何设置主窗口的初始位置? [英] Qt: how to set main window's initial position?

查看:4726
本文介绍了Qt:如何设置主窗口的初始位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为正常的窗口管理器确定QMainWindow在桌面上的位置的初始位置。我想自己设置初始位置。

I think the normally window manager determines the initial position of the QMainWindow position on the desk top. I want to set the initial position myself. How is this done with Qt on Windows?

推荐答案

您可以使用 restoreGeometry(),以及具有 restoreState() ...

You can restore the window geometry with restoreGeometry(), and the state of docked elements with restoreState(), during the construction of your MainWindow...

    QSettings settings("yourcompany", "yourapp");

    restoreGeometry(settings.value("geometry").toByteArray());
    restoreState(settings.value("state").toByteArray(),YOUR_UI_VERSION);

然后,如果您覆写 closeEvent(),您可以按如下方式保存状态:

Then, if you override closeEvent(), you can save the state as follows:

    QSettings settings("yourcompany", "yourapp");

    settings.setValue("geometry", saveGeometry());
    settings.setValue("state", saveState(YOUR_UI_VERSION));

YOUR_UI_VERSION是一个常数,当UI变化很大时应该增加,以防止尝试恢复无效状态。

YOUR_UI_VERSION is a constant you should increment when your UI changes significantly to prevent attempts to restore an invalid state.

这篇关于Qt:如何设置主窗口的初始位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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