如何在 Qt 中存储会话之间的窗口大小?

发布于 2024-07-05 01:54:25 字数 181 浏览 7 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

农村范ル 2024-07-12 01:54:26

我也遇到过这个问题。

你可以做什么:除了窗口的大小之外,还保存它是否最大化(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.

始于初秋 2024-07-12 01:54:26

图像位于 http://qt-project.org/doc/qt- 4.8/application-windows.html 显示,geometry.x()geometry.y() 不等于 x()< /code> 和 y(),与 pos() 相同。

就我而言,我使用:

x()
y()
width()
height()

并通过以下方式成功恢复它们:

move()
resize()

The image at http://qt-project.org/doc/qt-4.8/application-windows.html shows, that geometry.x() and geometry.y() are not equal to x() and y(), which are the same as pos().

In my case, I use:

x()
y()
width()
height()

and restore these successfully with:

move()
resize()
巴黎盛开的樱花 2024-07-12 01:54:26

使用 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.

情绪操控生活 2024-07-12 01:54:25

我发现在 Fedora 14 上需要结合前面所有的答案。小心不要在窗口最大化时保存大小和位置!

void MainWindow::writePositionSettings()
{
    QSettings qsettings( "iforce2d", "killerapp" );

    qsettings.beginGroup( "mainwindow" );

    qsettings.setValue( "geometry", saveGeometry() );
    qsettings.setValue( "savestate", saveState() );
    qsettings.setValue( "maximized", isMaximized() );
    if ( !isMaximized() ) {
        qsettings.setValue( "pos", pos() );
        qsettings.setValue( "size", size() );
    }

    qsettings.endGroup();
}

void MainWindow::readPositionSettings()
{
    QSettings qsettings( "iforce2d", "killerapp" );

    qsettings.beginGroup( "mainwindow" );

    restoreGeometry(qsettings.value( "geometry", saveGeometry() ).toByteArray());
    restoreState(qsettings.value( "savestate", saveState() ).toByteArray());
    move(qsettings.value( "pos", pos() ).toPoint());
    resize(qsettings.value( "size", size() ).toSize());
    if ( qsettings.value( "maximized", isMaximized() ).toBool() )
        showMaximized();

    qsettings.endGroup();
}

在 main() 中,在第一次显示窗口之前读取位置设置......

MainWindow mainWindow;
mainWindow.readPositionSettings();
mainWindow.show();

并且这些事件处理程序根据需要更新设置。 (这会导致在移动和调整大小期间每次鼠标移动都会写入设置文件,这并不理想。)

void MainWindow::moveEvent( QMoveEvent* )
{
    writePositionSettings();
}

void MainWindow::resizeEvent( QResizeEvent* )
{
    writePositionSettings();
}

void MainWindow::closeEvent( QCloseEvent* )
{
    writePositionSettings();
}

不过,位置的垂直分量不太正确,它似乎忽略了窗口标题栏的高度。 ..如果有人知道如何处理,请告诉我:)

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!

void MainWindow::writePositionSettings()
{
    QSettings qsettings( "iforce2d", "killerapp" );

    qsettings.beginGroup( "mainwindow" );

    qsettings.setValue( "geometry", saveGeometry() );
    qsettings.setValue( "savestate", saveState() );
    qsettings.setValue( "maximized", isMaximized() );
    if ( !isMaximized() ) {
        qsettings.setValue( "pos", pos() );
        qsettings.setValue( "size", size() );
    }

    qsettings.endGroup();
}

void MainWindow::readPositionSettings()
{
    QSettings qsettings( "iforce2d", "killerapp" );

    qsettings.beginGroup( "mainwindow" );

    restoreGeometry(qsettings.value( "geometry", saveGeometry() ).toByteArray());
    restoreState(qsettings.value( "savestate", saveState() ).toByteArray());
    move(qsettings.value( "pos", pos() ).toPoint());
    resize(qsettings.value( "size", size() ).toSize());
    if ( qsettings.value( "maximized", isMaximized() ).toBool() )
        showMaximized();

    qsettings.endGroup();
}

In main(), the position settings are read before showing the window the first time...

MainWindow mainWindow;
mainWindow.readPositionSettings();
mainWindow.show();

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

void MainWindow::moveEvent( QMoveEvent* )
{
    writePositionSettings();
}

void MainWindow::resizeEvent( QResizeEvent* )
{
    writePositionSettings();
}

void MainWindow::closeEvent( QCloseEvent* )
{
    writePositionSettings();
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文