我在Pyside工作。每次我重新打开窗口时,它都会弹回到屏幕中间。如何让Maya或Windows记住位置和大小?
以下是我正在使用的一些基本代码:
import traceback
from PySide import QtCore
from PySide import QtGui
from shiboken import wrapInstance
import maya.cmds as cmds
import maya.OpenMayaUI as omui
import pymel.core as pm
import maya.cmds as cmds
def maya_main_window():
'''
Return the Maya main window widget as a Python object
'''
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(long(main_window_ptr), QtGui.QWidget)
class TestTool(QtGui.QDialog):
def __init__(self, parent=maya_main_window()):
super(TestTool, self).__init__(parent)
self.qtSignal = QtCore.Signal()
#################################################################
def create(self):
'''
Set up the UI prior to display
'''
self.setWindowTitle("Test")
self.setWindowFlags(QtCore.Qt.Tool)
#self.resize(400, 250) # re-size the window
self.setGeometry(650, 200, 600, 300)
self.setFixedHeight(580)
self.setFixedWidth(300)
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
if __name__ == "__main__":
try:
ui.deleteLater()
except:
pass
ui = TestTool()
try:
ui.create()
ui.show()
except:
ui.deleteLater()
traceback.print_exc()
答案 0 :(得分:1)
您可以使用的一个选项是QWidget.saveGeometry()
和QWidget.restoreGeometry()
。使用此功能,您可以在工具关闭时保存窗口的位置和大小,然后在初始化时将其恢复。
通常对于这样的东西,它保存工具的状态,我会将数据存储到QtCore.QSettings
的某处的ini文件中。这样,即使您关闭Maya,或者有多个会话正在运行,它也会恢复到最后一个状态。
以下是一个例子:
import traceback
from PySide import QtCore
from PySide import QtGui
from shiboken import wrapInstance
import maya.cmds as cmds
import maya.OpenMayaUI as omui
import pymel.core as pm
import maya.cmds as cmds
import os
def maya_main_window():
'''
Return the Maya main window widget as a Python object
'''
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(long(main_window_ptr), QtGui.QWidget)
class TestTool(QtGui.QDialog):
def __init__(self, parent=maya_main_window()):
super(TestTool, self).__init__(parent)
self.qtSignal = QtCore.Signal()
# Using an env variable makes the path more generic, but use whatever you want
self.settings_path = os.path.join(os.getenv('HOME'), "settingsFile.ini")
#################################################################
def create(self):
'''
Set up the UI prior to display
'''
self.setWindowTitle("Test")
self.setWindowFlags(QtCore.Qt.Tool)
self.resize(400, 250) # re-size the window
self.setGeometry(650, 200, 600, 300)
self.setFixedHeight(580)
self.setFixedWidth(300)
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
# Restore window's previous geometry from file
if os.path.exists(self.settings_path):
settings_obj = QtCore.QSettings(self.settings_path, QtCore.QSettings.IniFormat)
self.restoreGeometry(settings_obj.value("windowGeometry"))
def closeEvent(self, event):
# Save window's geometry
settings_obj = QtCore.QSettings(self.settings_path, QtCore.QSettings.IniFormat)
settings_obj.setValue("windowGeometry", self.saveGeometry())
if __name__ == "__main__":
try:
ui.deleteLater()
except:
pass
ui = TestTool()
try:
ui.create()
ui.show()
except:
ui.deleteLater()
traceback.print_exc()
由于您要将尺寸设置为固定,因此这只会影响位置。希望它有所帮助!