I have a simple problem with QtDockWidget in PySide2. Whenever I execute my script it creates the UI widget but every new script call creates one more instance of the same UI object instead of showing the already existing one. It's the same situation with demoPySideToolBarQWidget.py example. How do I check and call the UI object which appears under customize ui rollout?
Solved! Go to Solution.
Solved by drew.avis. Go to Solution.
You could check to see if the Max main window already holds an instance of your widget, and only create a new instance if it doesn't. Here's an example of this approach:
from PySide2 import QtCore from PySide2 import QtGui from PySide2 import QtWidgets import pymxs import MaxPlus def make_cylinder(): cyl = pymxs.runtime.Cylinder(radius=10, height=30) pymxs.runtime.redrawViews() return class PyMaxDockWidget(QtWidgets.QDockWidget): def __init__(self, parent=None): super(PyMaxDockWidget, self).__init__(parent) self.setWindowFlags(QtCore.Qt.Tool) self.setWindowTitle('Pyside Qt Dock Window') self.initUI() self.setAttribute(QtCore.Qt.WA_DeleteOnClose) def initUI(self): main_layout = QtWidgets.QVBoxLayout() label = QtWidgets.QLabel("Click button to create a cylinder in the scene") main_layout.addWidget(label) cylinder_btn = QtWidgets.QPushButton("Cylinder") cylinder_btn.clicked.connect(make_cylinder) main_layout.addWidget(cylinder_btn) widget = QtWidgets.QWidget() widget.setLayout(main_layout) self.setWidget(widget) self.resize(250, 100) def main(): pymxs.runtime.resetMaxFile(pymxs.runtime.name('noPrompt')) mainWindow = MaxPlus.GetQMaxMainWindow()
# check if there's already an instance running dockWidgets = [x for x in mainWindow.children() if x.__class__.__name__ == 'PyMaxDockWidget'] w = None if (len (dockWidgets)>0): w = dockWidgets[0] else: w = PyMaxDockWidget(parent=mainWindow) # Do this if you want to dock the widget initially: # mainWindow.addDockWidget(QtCore.Qt.LeftDockWidgetArea, w) w.setFloating(True) w.show() if __name__ == '__main__': main()