PyQT and Parenting Issue

24 views
Skip to first unread message

David Martinez

unread,
Mar 16, 2014, 1:06:13 PM3/16/14
to python_in...@googlegroups.com
Hello there,

I'm watching Justin Israel's video on PyQT and I have the following question:

Why is it that if a 'QPushbutton' is created with a widget as a parent it works fine but if I create the button first and then parent it using the 'setParent' method of a 'QObject', it doesn't?

Here is the code that I'm executing with iPython (where I already have an event loop working)


I'm just trying to understand the specifics of why that doesn't work.
I've tried printing the parent of both buttons and both of them are children of the widget.


--
David Martinez - Technical Animator
 

Justin Israel

unread,
Mar 16, 2014, 2:47:34 PM3/16/14
to python_in...@googlegroups.com
Hey David,

Actually your parenting should be working just fine either way. The problem you are seeing is that because the widgets are not in any kind of layout, they end up being positioned at (0,0) of their parent... on top of each other. The last one you created will be on top.

Try adding this line to the end of your code, and you should end up seeing both of them:

    button2.move(0, button2.height())

If you want to see the effect of a layout, you can add this to the end of your code:

    vlayout = QtGui.QVBoxLayout(widget)
    vlayout.addWidget(button)
    vlayout.addWidget(button2)

-- justin




--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAMLeNpzu2%2BPOs5pZypwGuBvkXX_zb7w0cp7nvsyV8tDCcj8eVw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

David Martinez

unread,
Mar 16, 2014, 2:56:43 PM3/16/14
to python_in...@googlegroups.com
Right!

I didn't think that since I wasn't using a layout, I would end up with both of them sitting on top of each other.
(I should have tried to draw one, and then the other to verify that they both were getting properly parented).

Thanks again for coming to the rescue Justin!



--
David Martinez - Technical Animator
 


Reply all
Reply to author
Forward
0 new messages