如何记住PyQt应用程序的最后几何形状? [英] How to remember last geometry of PyQt application?
问题描述
我在Windows 8.1
64位上使用PyQt5 5.5.1(64位)和Python 3.4.0(64位)。
I am using PyQt5 5.5.1 (64-bit) with Python 3.4.0 (64-bit) on Windows 8.1 64-bit.
我在恢复非常简单的PyQt应用程序
的位置和大小(几何形状)时遇到麻烦。
I am having trouble restoring the position and size (geometry) of my very simple PyQt app.
这是最小的工作应用程序:
Here is minimal working application:
import sys
from PyQt5.QtWidgets import QApplication, QWidget
class myApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
view = myApp()
sys.exit(app.exec())
我在网上阅读的是这是默认行为,我们需要
使用QSettings保存和检索Windows注册表中的设置,
存储在
What I read online is that this is the default behavior and we need to use QSettings to save and retrieve settings from Windows registry, which is stored in
\\HKEY_CURRENT_USER\Software\{CompanyName}\{AppName}\
遵循了这些教程,但是那些教程/文档是为C ++用户编写的
。
I could have followed those tutorials but those tutorials/docs were written for C++ users.
C ++不是我的杯水,转换这些代码对我来说是不可能的。
C++ is not my glass of beer, and converting those codes are impossible to me.
推荐答案
应该这样做。
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QSettings, QPoint, QSize
class myApp(QWidget):
def __init__(self):
super(myApp, self).__init__()
self.settings = QSettings( 'My company', 'myApp')
# Initial window size/pos last saved. Use default values for first time
self.resize(self.settings.value("size", QSize(270, 225)))
self.move(self.settings.value("pos", QPoint(50, 50)))
def closeEvent(self, e):
# Write window size and position to config file
self.settings.setValue("size", self.size())
self.settings.setValue("pos", self.pos())
e.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
frame = myApp()
frame.show()
app.exec_()
我简化了此示例: QSettings():如何保存到当前工作目录
这篇关于如何记住PyQt应用程序的最后几何形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!