Hy guy,PyQt 4 : 툴바 위치 가져 오기
내 실행 프로그램에는 도구 모음이 있습니다. 사용자는 툴바를 이동하기로 결정합니다. 이제 툴바가 떠있었습니다. 툴바가 사용자에 의해 배열 될 때 나는 떠있는 신호를 연결해야한다는 것을 압니다. 툴바의 새로운 위치를 저장하려면 어떻게해야합니까? 주 윈도우에 툴바를 추가하는 방법을 위치 : self.addToolBar(Qt.LeftToolBarArea , toolbar_name)
으로 알고 있습니다. handle_floating() - 메서드에서 내가 원하는 것을 볼 수 있습니다 : 거기서 현재 위치를 얻고 싶습니다. 그러나 어떻게? 또한 방금 도구 막대의 위치를 유지하기 위해 self.toolbar_pos
이라는 멤버 변수 하나를 추가 한 것을 볼 수 있습니다. 내 아이디어는 응용 프로그램이 종료되면이 값을 파일에 직렬화하고 나중에 응용 프로그램을 다시 실행하면 해당 파일을 읽고 이에 따라 도구 모음을 설정한다는 것입니다. 그러나 이것은 아무런 문제가되지 않습니다. 현재 저는 툴바의 위치를 잡을 생각이 없습니다. saveState 및 restoreState 즉 :
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.toolbar_pos = None
self.initUI()
def initUI(self):
exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(QtGui.qApp.quit)
self.toolbar = QtGui.QToolBar(self)
self.toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
self.addToolBar(self.toolbar)
self.toolbar.addAction(exitAction)
self.toolbar.setAllowedAreas(QtCore.Qt.TopToolBarArea
| QtCore.Qt.BottomToolBarArea
| QtCore.Qt.LeftToolBarArea
| QtCore.Qt.RightToolBarArea)
self.addToolBar(QtCore.Qt.LeftToolBarArea , self.toolbar)
self.toolbar.topLevelChanged.connect(self.handle_floating)
def handle_floating(self, event):
# The topLevel parameter is true
# if the toolbar is now floating
if not event:
# If the toolbar no longer floats,
# then calculate the position where the
# toolbar is located currently.
self.toolbar_pos = None
print "Get position: ?"
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.setGeometry(300, 300, 300, 200)
ex.setWindowTitle('Toolbar example')
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
솔루션에 매우 유용합니다
마지막으로, 당신은 이전에 저장된 상태를 복원 할 수 있습니다. 고마워. 하지만 약간의 질문이 있습니다. 'closeEvent() - method'를 덮어 쓰면 메인 윈도우의 상태를 파일에 저장하려고합니다. 자, 왜 .data() 함수를 사용하고 있습니까? – Sophus
@Sophus. 'saveState' 메쏘드는'QByteArray'를 리턴합니다. python/pyqt의 일부 버전에서는 python을 사용하여 파일에 직접 작성할 수 있지만 다른 경우에는 python을 사용하지 않을 수 있습니다. 'QByteArray'의'data()'메소드는 그것을 파이썬'bytes' 객체로 변환합니다 - 이는 모든 버전의 python/pyqt에서 작동한다는 것을 의미합니다. 그러나'QSettings'을 사용하여 상태를 저장하면 파일 쓰기가 모두 Qt에 의해 이루어지기 때문에 이러한 호환성 문제에 대해 걱정할 필요가 없습니다. – ekhumoro
@Sophus. 추신 : 실제로, 전체 호환성을 위해'restoreState' 메서드는 아마도 QByteArray를 명시 적으로 사용해야합니다 (따라서 필자의 대답을 업데이트했습니다). – ekhumoro