Maya/PyQt Wiget Auto-Size Problem

Hello all! I’m playing around in Maya with PyQt and with all the helpful posts I’ve been able to make great progress. I’ve run into an issue dealing with QWidgets not resizing properly. The following code is a small example, though it’s a little contrived. Basically the QScrollArea remains at a fixed size. I’ve read much of the documentation on QSizePolicy and attempted to set the policy for both the scroll area and the formlayout to which it’s parented, but it did not work. Also, I understand that in Qt, a layout is NOT a widget, however in Maya the underlying obect IS a QWidget. This is probably adding to my confusion.

I’d like the scroll area to change accordingly when the window is re-sized.

So what am i doing wrong?

from PyQt4 import QtGui, QtCore
import maya.OpenMayaUI as mui
import sip
import maya.cmds as cmds
import os

def getControl(controlName):
    ptr = mui.MQtUtil.findControl(controlName)
    return sip.wrapinstance(long(ptr), QtCore.QObject)

def getLayout(layoutName):
    ptr = mui.MQtUtil.findControl(layoutName)
    return sip.wrapinstance(long(ptr), QtCore.QObject)

#Create our QWidgets and Layout
m_scrollArea = QtGui.QScrollArea()


#Create the window
cmds.window()

#Add PaneLayout
m_paneLayout = cmds.paneLayout( configuration='vertical2' )
#Add a standard Maya Button to the left Pane
cmds.button()
#Add a formLayout fo the right Pane
m_FormLayout = cmds.formLayout()
cmds.setParent('..')
cmds.showWindow()

#Get the name of the control in the 2'd pane
m_SecondPaneName = cmds.paneLayout(m_paneLayout, q=True, p2=True)
#Grab a reference to the 2nd panes control
m_QPane = getLayout(m_SecondPaneName)

#Set the scroll area's parent to the 2n Panes control
m_scrollArea.setParent(m_QPane)
#Show the scroll area
m_scrollArea.show()

Thanks!

  • Luke

Alright, fixed this one with some good old trial and error. The fix was two parts. First, instead of setting the parent of the widget to the control, I added the widget to the parents layout. This gave me more reasonable behavior as well as made more sense logically. However, my ScrollArea was still not re-sizing with my window.

Old Way:

m_scrollArea.setParent(m_QPane)

New Way:

m_QPane.layout().addWidget(m_scrollArea)

Then, to actually fix the sizing problem, i used a rowColumnLayout instead of a formLayot and set flags accordingly

Old:

#Add PaneLayout
m_paneLayout = cmds.paneLayout( configuration='vertical2' )
#Add a standard Maya Button to the left Pane
cmds.button()
#Add a formLayout fo the right Pane
m_FormLayout = cmds.formLayout()
cmds.setParent('..')
cmds.showWindow()

New:

#Add PaneLayout
m_paneLayout = cmds.paneLayout( configuration='vertical2' )
#Add a standard Maya Button to the left Pane
cmds.button()
#Add a rowLayoutfo the right Pane
m_FormLayout = cmds.rowLayout(nc=1, rat=(1, 'both', 0), adj=1)
cmds.setParent('..')
cmds.showWindow()

So now it works!