我无法弄清楚保存和恢复是如何工作的,因此当应用程序关闭并再次打开时,我可以保存dockwidgets几何。我有5个dockwidgets,我希望有这个功能。
我假设我必须分别在restoreState
和saveState
使用init
和close
。但是我该如何配置呢?
MainWindow类
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from DockWindowGraph import Dock
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.centralWindow_ = QFrame()
self.setCentralWidget(None)
self.CreateWidgets()
self.settings = QSettings()
self.restoreState()
def CreateWidgets(self):
self.toolbar = self.addToolBar('Toolbar')
self.toolbar.setMovable(False)
exitA = QAction(QIcon('Images/gj.png'), 'Exit', self)
exitA.setShortcut('Ctrl+Q')
exitA.setStatusTip('Exit application')
exitA.triggered.connect(self.close)
self.toolbar.addAction(exitA)
openDock_ = QAction(QIcon('Images/gj.png'), 'Open', self)
openDock_.setShortcut('Ctrl+E')
openDock_.setStatusTip('Open Dock')
openDock_.triggered.connect(self.OpenDockWindow)
self.toolbar.addAction(openDock_)
self.setWindowTitle("We do not sow")
self.showFullScreen()
self.firstDock_ = Dock(self, 'First')
self.firstDock_.setObjectName('First')
self.addDockWidget(Qt.LeftDockWidgetArea, self.firstDock_)
self.secondDock_ = Dock(self, 'Second')
self.firstDock_.setObjectName('Second')
self.addDockWidget(Qt.LeftDockWidgetArea, self.secondDock_)
self.thirdDock_ = Dock(self, 'Third')
self.thirdDock_.setObjectName('Third')
self.splitDockWidget(self.firstDock_, self.thirdDock_, Qt.Horizontal)
self.fDock_ = Dock(self, 'Fourth')
self.fDock_.setObjectName('Fourth')
self.splitDockWidget(self.firstDock_, self.fDock_, Qt.Horizontal)
self.fiDock_ = Dock(self, 'Fifth')
self.fiDock_.setObjectName('Fifth')
self.splitDockWidget(self.firstDock_, self.fiDock_, Qt.Vertical)
self.setTabPosition(Qt.AllDockWidgetAreas, QTabWidget.North)
def OpenDockWindow(self):
dock_ = Dock((self.frameGeometry().width() / 2), self.firstDock_)
self.addDockWidget(Qt.RightDockWidgetArea, dock_)
self.tabifyDockWidget(self.secondDock_, dock_)
def closeEvent(self, event):
print('closing')
settings_ = QSettings()
self.saveState()
app = QApplication(sys.argv)
app.setOrganizationDomain('ltd')
app.setOrganizationName('Alg')
w = Window()
sys.exit(app.exec_())
我的Dock类:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from DockWindowFrame import Frame
class Dock(QDockWidget):
def __init__(self, title, parent=None):
super(Dock, self).__init__(parent)
self.setAllowedAreas(Qt.RightDockWidgetArea | Qt.LeftDockWidgetArea | Qt.TopDockWidgetArea | Qt.BottomDockWidgetArea)
self.setFeatures(QDockWidget.DockWidgetFloatable | QDockWidget.DockWidgetMovable )
self.frame = Frame()
self.frame.setStyleSheet("QWidget { background-color: rgba(0,220,0,100%)}")
self.setWidget(self.frame)
def ReturnFrame(self):
return self.frame
编辑:尝试以下答案后出错。
Traceback (most recent call last):
File "C:\Users\Rasmus\workspace\PyQtLearning\src\gg.py", line 90, in <module>
main()
File "C:\Users\Rasmus\workspace\PyQtLearning\src\gg.py", line 86, in main
w = Window()
File "C:\Users\Rasmus\workspace\PyQtLearning\src\gg.py", line 14, in __init__
self.readSettings()
File "C:\Users\Rasmus\workspace\PyQtLearning\src\gg.py", line 72, in readSettings
self.restoreGeometry(settings.value("geometry").toByteArray())
AttributeError: 'NoneType' object has no attribute 'toByteArray'
答案 0 :(得分:2)
这可以在这里工作:
def __init__(self):
self.readSettings()
def closeEvent(self, event):
print('closing')
settings = QSettings()
settings.setValue('geometry',self.saveGeometry())
settings.setValue('windowState',self.saveState())
super(Window, self).closeEvent(event)
def readSettings(self):
settings = QSettings()
self.restoreGeometry(settings.value("geometry").toByteArray())
self.restoreState(settings.value("windowState").toByteArray())
参考: QMainWindow.saveState() QMainWindow.restoreState()
完整代码:
# coding = u8
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.centralWindow_ = QFrame()
self.setCentralWidget(None)
self.CreateWidgets()
self.readSettings()
def CreateWidgets(self):
self.toolbar = self.addToolBar('Toolbar')
self.toolbar.setMovable(False)
exitA = QAction(QIcon('Images/gj.png'), 'Exit', self)
exitA.setShortcut('Ctrl+Q')
exitA.setStatusTip('Exit application')
exitA.triggered.connect(self.close)
self.toolbar.addAction(exitA)
openDock_ = QAction(QIcon('Images/gj.png'), 'Open', self)
openDock_.setShortcut('Ctrl+E')
openDock_.setStatusTip('Open Dock')
openDock_.triggered.connect(self.OpenDockWindow)
self.toolbar.addAction(openDock_)
self.setWindowTitle("We do not sow")
self.showFullScreen()
self.firstDock_ = Dock(self, 'First')
self.firstDock_.setObjectName('First')
self.addDockWidget(Qt.LeftDockWidgetArea, self.firstDock_)
self.secondDock_ = Dock(self, 'Second')
self.firstDock_.setObjectName('Second')
self.addDockWidget(Qt.LeftDockWidgetArea, self.secondDock_)
self.thirdDock_ = Dock(self, 'Third')
self.thirdDock_.setObjectName('Third')
self.splitDockWidget(self.firstDock_, self.thirdDock_, Qt.Horizontal)
self.fDock_ = Dock(self, 'Fourth')
self.fDock_.setObjectName('Fourth')
self.splitDockWidget(self.firstDock_, self.fDock_, Qt.Horizontal)
self.fiDock_ = Dock(self, 'Fifth')
self.fiDock_.setObjectName('Fifth')
self.splitDockWidget(self.firstDock_, self.fiDock_, Qt.Vertical)
self.setTabPosition(Qt.AllDockWidgetAreas, QTabWidget.North)
def OpenDockWindow(self):
dock_ = Dock((self.frameGeometry().width() / 2), self.firstDock_)
self.addDockWidget(Qt.RightDockWidgetArea, dock_)
self.tabifyDockWidget(self.secondDock_, dock_)
def closeEvent(self, event):
print('closing')
settings = QSettings()
settings.setValue('geometry',self.saveGeometry())
settings.setValue('windowState',self.saveState())
super(Window, self).closeEvent(event)
def readSettings(self):
settings = QSettings()
self.restoreGeometry(settings.value("geometry").toByteArray())
self.restoreState(settings.value("windowState").toByteArray())
class Dock(QDockWidget):
def __init__(self, title, parent=None):
super(Dock, self).__init__(parent)
self.setAllowedAreas(Qt.RightDockWidgetArea | Qt.LeftDockWidgetArea | Qt.TopDockWidgetArea | Qt.BottomDockWidgetArea)
self.setFeatures(QDockWidget.DockWidgetFloatable | QDockWidget.DockWidgetMovable )
def main():
app = QApplication(sys.argv)
app.setOrganizationDomain('ltd')
app.setOrganizationName('Alg')
w = Window()
sys.exit(app.exec_())
if __name__ == '__main__':
main()