[PySide] restricting a widget to only be visible inside it's layout's geometry

Aaron Richiger a.richi at bluewin.ch
Fri Jun 29 18:14:57 CEST 2012

Hello Frank!

This time, I'm not quite sure, whether I understood your task 
correctly... If I got it right, you want the button to be visible only 
within the frame box!? A widget is only visible within it's parents 
geometry, so you can use this behaviour. In your short example, the 
layouts and buttons parent is the mainWidget an have therefore the same 
geometry as the mainWidget. I changed the code slightly, you will find 
it below. Please tell me, if this is not, what you were after! 
Additionally, note the following:

- (Knowing, that this may be the beginning of a holy war :-)): in 
python, you don't write spaces after opening and before closing brackets 
(see PEP8 or the Google Python Style Guide).
- I replaced your setParent() calls by using the constructors parameter. 
This makes the code shorter and is to prefer in my opinion, most 
PySiders do it this way, but again, this is just a question of personal 
taste... :-)


########## Code ###########

import sys
from PySide.QtGui import *
from PySide.QtCore import *

app = QApplication( sys.argv )

mainWidget = QWidget()
frame = QFrame(mainWidget)
frame.setFrameStyle( QFrame.Box )
label = QPushButton( 'this should only be visible inside the layout 
geo', frame )
label.resize( 500, 50 )

layout = QVBoxLayout( mainWidget )
layout.addWidget( frame )

mainWidget.resize( 500, 200 )
sys.exit( app.exec_() )

More information about the PySide mailing list