Show existing QDockWidget instead of initializing new one

Show existing QDockWidget instead of initializing new one

chrisdawlud
Enthusiast Enthusiast
1,403 Views
3 Replies
Message 1 of 4

Show existing QDockWidget instead of initializing new one

chrisdawlud
Enthusiast
Enthusiast

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?hdrimanager_problem.jpg

0 Likes
Accepted solutions (1)
1,404 Views
3 Replies
Replies (3)
Message 2 of 4

elpie89
Advocate
Advocate

Did you ever found a solution for this?

I have the exact same problem

0 Likes
Message 3 of 4

drew_avis
Autodesk
Autodesk
Accepted 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()


Drew Avis
Content Experience Designer
Message 4 of 4

elpie89
Advocate
Advocate

thanks

this worked, as I'm working on a larger tool, I need to attach and remove widget dynamically but, I think I found a bug

Can you take a look at this as well?

 

0 Likes