windows Qt:如何设置主窗口的初始位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/866970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt: how to set main window's initial position?
提问by Paul Dixon
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?
我认为通常的窗口管理器决定了桌面上 QMainWindow 位置的初始位置。我想自己设置初始位置。这是如何在 Windows 上使用 Qt 完成的?
回答by Paul Dixon
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 发生显着变化时,您应该增加它以防止尝试恢复无效状态。
回答by John T
I believe you're looking for setGeometry.
我相信您正在寻找setGeometry。