ウィンドウのジオメトリを復元する
このドキュメントでは、ジオメトリ プロパティを使用してウィンドウのジオメトリを保存および復元する方法について説明します。Windowsでは、基本的にQWindow::geometry ()の結果を保存し、次のセッションでQWindow::setGeometry ()を呼び出してからshow ()を呼び出します。
X11では、不可視ウィンドウはまだフレームを持っていないため、これは機能しないかもしれません。ウィンドウ・マネージャは後でウィンドウを装飾します。このとき、ウィンドウは装飾フレームのサイズに応じて画面の下隅や右隅に移動します。Xはこのずれを回避する方法を提供していますが、一部のウィンドウ・マネージャーはこの機能を実装していません。
を使用する場合 Qt Widgetsを使用する場合、Qt はウィジェット・ウィンドウのジオメトリと状態を保存・復元する関数を提供します。QWidget::saveGeometry() はウィンドウのジオメトリと最大化/フルスクリーン状態を保存し、QWidget::restoreGeometry() はそれを復元します。リストア関数は、リストアされたジオメトリが利用可能なスクリーン・ジオメトリの外にあるかどうかもチェックし、外にある場合は適切に修正します:
void MyMainWindow::closeEvent(QCloseEvent *event) { QSettings settings("MyCompany", "MyApp"); settings.setValue("geometry", saveGeometry()); settings.setValue("windowState", saveState()); QMainWindow::closeEvent(event); } void MainWindow::readSettings() { QSettings settings("MyCompany", "MyApp"); restoreGeometry(settings.value("myWidget/geometry").toByteArray()); restoreState(settings.value("myWidget/windowState").toByteArray()); }
もう1つの解決策は、pos ()とsize ()の両方を保存し、QWidget::resize ()とmove ()を使用してジオメトリを復元してから、show ()を呼び出すことである。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.