Qt记住上次窗口的位置和状态

#include <QCloseEvent>

#include <QShowEvent>

void MainWindow::showEvent(QShowEvent * event) {

          
restoreGeometry(_config->IniRead(QStringLiteral("MainWindow/geometry")).toByteArray()); // 读取位置
   

restoreState(_config->IniRead(QStringLiteral("MainWindow/state")).toByteArray());       // 读取状态
}


void MainWindow::closeEvent(QCloseEvent * event) {

          
 _config->IniWrite(QStringLiteral("MainWindow/geometry"), saveGeometry());   // 记住位置


 _config->IniWrite(QStringLiteral("MainWindow/state"), saveState());     // 记住状态
}

void MainWindow::showEvent(QShowEvent * event) {
restoreGeometry(_config->IniRead(QStringLiteral("MainWindow/geometry")).toByteArray()); // 读取位置
    restoreState(_config->IniRead(QStringLiteral("MainWindow/state")).toByteArray());       // 读取状态
}


void MainWindow::closeEvent(QCloseEvent * event) {
    _config->IniWrite(QStringLiteral("MainWindow/geometry"), saveGeometry());   // 记住位置
    _config->IniWrite(QStringLiteral("MainWindow/state"), saveState());     // 记住状态
}

=====================

官方手册

bool QWidget::restoreGeometry(const QByteArray &geometry)

Restores the geometry and state of top-level widgets stored in the byte array geometry. Returns true on success; otherwise returns false.

If the restored geometry is off-screen, it will be modified to be inside the available screen geometry.

To restore geometry saved using QSettings, you can use code like this:

QSettings settings("MyCompany", "MyApp");         myWidget->restoreGeometry(settings.value("myWidget/geometry").toByteArray());

See the Window Geometry documentation for an overview of geometry issues with windows.

Use QMainWindow::restoreState() to restore the geometry and the state of toolbars and dock widgets.

This function was introduced in Qt 4.2.

See also saveGeometry(), QSettings, QMainWindow::saveState(), and QMainWindow::restoreState().

bool QMainWindow::restoreState(const QByteArray &state, int version = 0)


Restores the state of this mainwindow's toolbars and dockwidgets. Also restores the corner settings too. The version number is compared with that stored in state. If they do not match, the mainwindow's state is left unchanged, and this function returns false; otherwise, the state is restored, and this function returns true.
To restore geometry saved using QSettings, you can use code like this:

  void MainWindow::readSettings()
  {
      QSettings settings("MyCompany", "MyApp");
      restoreGeometry(settings.value("myWidget/geometry").toByteArray());
      restoreState(settings.value("myWidget/windowState").toByteArray());
  }

See also saveState(), QWidget::saveGeometry(), QWidget::restoreGeometry(), and restoreDockWidget().

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值