简体   繁体   English

Maya PySide Window - 记住位置和大小

[英]Maya PySide Window - Remember position and size

I am working in Pyside.我在 Pyside 工作。 Every time I re-open the window it pops back to the middle of the screen.每次我重新打开窗口时,它都会弹回到屏幕中间。 How can I get either Maya or Windows to remember the position and size?如何让 Maya 或 Windows 记住位置和大小?

Here is some basic code I am working with:这是我正在使用的一些基本代码:

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()

One option you can use is QWidget.saveGeometry() and QWidget.restoreGeometry() .您可以使用的一种选择是QWidget.saveGeometry()QWidget.restoreGeometry() With this you can save your window's position and size when your tool closes, then restore it back when it initializes.有了这个,您可以在工具关闭时保存窗口的位置和大小,然后在初始化时将其恢复。

Normally for stuff like this where it's saving a state of the tool, I'll store the data to an ini file somewhere with QtCore.QSettings .通常对于这种保存工具状态的东西,我会将数据存储到一个带有QtCore.QSettings的 ini 文件中。 This way it will restore to the last state even if you close Maya, or have multiple sessions running.这样,即使您关闭 Maya 或运行多个会话,它也会恢复到最后一个状态。

Here's an example:下面是一个例子:

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()

Since you're setting the size to be fixed, this will only effect position.由于您将尺寸设置为固定,这只会影响位置。 Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM