简体   繁体   English

Qt:如何设置主窗口的初始位置?

[英]Qt: how to set main window's initial position?

I think the normally window manager determines the initial position of the QMainWindow position on the desk top. 我认为通常的窗口管理器确定桌面上QMainWindow位置的初始位置。 I want to set the initial position myself. 我想自己设定初始位置。 How is this done with Qt on Windows? 如何在Windows上使用Qt完成此操作?

You can restore the window geometry with restoreGeometry() , and the state of docked elements with restoreState() , during the construction of your MainWindow... 您可以在构造MainWindow的过程中使用restoreGeometry()恢复窗口几何形状,并使用restoreState()恢复停靠元素的状态。

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

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

Then, if you override closeEvent() , you can save the state as follows: 然后,如果覆盖closeEvent() ,则可以如下保存状态:

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

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

YOUR_UI_VERSION is a constant you should increment when your UI changes significantly to prevent attempts to restore an invalid state. YOUR_UI_VERSION是一个常数,在UI发生重大变化时应增加此常数,以防止尝试恢复无效状态。

我相信您正在寻找setGeometry

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM