Thanks for your help, Joe, it makes sense.

However resize() does work, even after assigning a layout to the parent.
menu.resize(500, 100) enlarges the menu enough to fit the buttons.

Altering size policies doesn't seem to have much of an effect here either.



from PyQt4.QtGui import *
from PyQt4.QtCore import *




class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)


        menu = QWidget(self)
        menu.setObjectName('Menu')
        # menu.resize(500, 200)


        btn1 = QPushButton('Hello')
        btn2 = QPushButton('World')


        for wid in (btn1, btn2, menu):
            wid.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.
MinimumExpanding)


        layout = QBoxLayout(QBoxLayout.LeftToRight, menu)
        layout.addWidget(btn1)
        layout.addWidget(btn2)
        layout.setSizeConstraint(QLayout.SetNoConstraint)


        layout = QBoxLayout(QBoxLayout.TopToBottom, self)


        self.setStyleSheet("#Menu {background-color: darkgray; }")
        self.resize(300, 100)


        
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)


    win = Window()
    win.show()


    sys.exit(app.exec_())




On Friday, 3 January 2014 17:19:54 UTC, Joe Weidenbach wrote:
>
>  From my understanding, once you're using a layout, the layout handles 
> sizing on all of your contained objects, and resize() doesn't do much at 
> all for you.  The trick here is to set your minimumSize() and maximumSize() 
> properties on the buttons, along with a look into QSizePolicy options for 
> both horizontal and vertical, and then the layout will work off of those to 
> handle sizing.  I'll see if I have time later today to give you a few 
> samples in case that doesn't make sense :).
>
> On 1/3/2014 4:33 AM, Marcus Ottosson wrote:
>  
> This little caveat has been bothering me for a few hours. 
>
>  Adding a widget as a child of a parent widget makes it position itself 
> at (0, 0). That's fine, I can move it around in resizeEvent. The size of 
> the widget is determined by its children - say if the widget is a container 
> for a couple of buttons, the amount of buttons and their default sizes 
> govern the size of the container.
>
>  But, installing a layout onto the parent of the container causes the 
> container to seemingly force its children to resize to an inappropriate 
> size.
>
>  I could imaging that assigning a layout to a parent with children might 
> implicitly assign its children to the layout, but that isn't what I would 
> expect. Ultimately, I would like to have a completely free-floating widget.
>
>  Uncomment the layout line to see what happens. This happens in both 
> PyQt4 and 5 so I expect to be missing something trivial.
>
>  
>  from PyQt4.QtGui import *
> from PyQt4.QtCore import *
>
>
>
>
> class Window(QWidget):
>     def __init__(self, parent=None):
>         super(Window, self).__init__(parent)
>
>
>         menu = QWidget(self)
>         menu.setObjectName('Menu')
>
>
>         btn1 = QPushButton('Hello')
>         btn2 = QPushButton('World')
>
>
>         layout = QBoxLayout(QBoxLayout.LeftToRight, menu)
>         layout.addWidget(btn1)
>         layout.addWidget(btn2)
>
>
>         # layout = QBoxLayout(QBoxLayout.TopToBottom, self)
>
>
>         self.setStyleSheet("#Menu {background-color: darkgray; }")
>         self.resize(300, 100)
>
>
>         
> if __name__ == '__main__':
>     import sys
>     app = QApplication(sys.argv)
>
>
>     win = Window()
>     win.show()
>
>
>     sys.exit(app.exec_())
>  
>   -- 
> You received this message because you are subscribed to the Google Groups 
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] <javascript:>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/python_inside_maya/240d1de8-ead1-455a-a344-ff991db44398%40googlegroups.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>  

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/74e8bdb2-64bd-4f43-9307-2a3ce802e18b%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to