如何解决有没有办法通过subproces.Popen发送PySide2模型?
在我的MainApp
中,我有一个QTreeView
,其中填充了QStandardItemModel
中的项目。通过单击按钮,我使用subprocess.Popen
启动3D软件(在本例中为Maya2020)。 Maya运行后,我将在其脚本编辑器中执行一段代码,以打开名为SubApp
的小部件。
我想以某种方式将MainApp
的模型数据传递到SubApp
的QTreeView。在启动SubApp
的真实代码中,我使用环境变量发送一些键值(UUID和UUID父级列表)来再次重建模型/视图。这感觉太复杂了,我想知道是否有更好的共享数据的方法?
我的代码:(注意!为简单起见,我将MainApp和SubApp包含在同一模块中,但这些类在实际中是分离的)
main_launch.py
import os
import sys
import subprocess
from Qt import QtWidgets,QtGui
class MainApp(QtWidgets.QWidget):
""" This is the main launcher app that holds the model data
"""
def __init__(self,parent=None):
super(MainApp,self).__init__(parent)
# Model/View
view = QtWidgets.QTreeView()
model = QtGui.QStandardItemModel()
view.setModel(model)
# Populate data
for i in range(3):
parent1 = QtGui.QStandardItem('Parent {}'.format(i))
for j in range(3):
child1 = QtGui.QStandardItem('Child {}'.format(i * 3 + j))
child2 = QtGui.QStandardItem('row: {},col: {}'.format(i,j + 1))
child3 = QtGui.QStandardItem('row: {},j + 2))
parent1.appendRow([child1,child2,child3])
model.appendRow(parent1)
# span container columns
view.setFirstColumnSpanned(i,view.rootIndex(),True)
# GUI
btn_launch = QtWidgets.QPushButton('launch sub app')
btn_launch.clicked.connect(self.on_launch_sub_app)
# Layout
main_layout = QtWidgets.QHBoxLayout(self)
main_layout.addWidget(view)
main_layout.addWidget(btn_launch)
self.setLayout(main_layout)
self.root_path = os.path.join(os.path.dirname(os.path.realpath(__file__))).replace('\\','/')
# Copy this path when it prints and paste it into "maya_snippet"s "root_path"
print(self.root_path)
def on_launch_sub_app(self):
""" Opens software (maya in this case) and then you launch SubApp from within maya's script editor.
Is there any way to send the model data using subprocess.Popen?
"""
# Maya location
app_exe = r'C:/Program Files/Autodesk/Maya2020/bin/maya.exe'
proc = subprocess.Popen(
[app_exe]
)
class SubApp(QtWidgets.QWidget):
""" Running the "maya_snippet.py" code from within maya will launch this window
"""
def __init__(self,parent=None):
super(SubApp,self).__init__(parent)
# Model/View
view = QtWidgets.QTreeView()
# Would like to set the model from the MainApp
model = QtGui.QStandardItemModel()
view.setModel(model)
# Layout
main_layout = QtWidgets.QHBoxLayout(self)
main_layout.addWidget(view)
self.setLayout(main_layout)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MainApp()
window.resize(400,400)
window.show()
sys.exit(app.exec_())
maya_snippet.py (在Maya中运行此程序以启动SubApp(从MainApp启动Maya之后))
import sys
# Change root_path to the directory holding main_launch.py
root_path = 'path/to/directory/holding/main_launch'
if not root_path in sys.path:
sys.path.append(root_path.replace('\\','/')
import main_launch
w = main_launch.SubApp()
w.show()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。