简体   繁体   中英

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. I want to set the initial position myself. How is this done with Qt on Windows?

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);

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 is a constant you should increment when your UI changes significantly to prevent attempts to restore an invalid state.

我相信您正在寻找setGeometry

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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