[Interest] How to produce a cluster of radio buttons?

Shriramana Sharma samjnaa at gmail.com
Fri Aug 31 07:54:25 CEST 2012


On Fri, Aug 31, 2012 at 8:28 AM, Syam Krishnan <syamcr at gmail.com> wrote:
> Try having another QWidget as the background parent widget of the
> buttons. Set it to have fixed size and set maximum (and minimum) size.
> Now you cat try to have a QGridLayout on it and add your radio buttons
> to the 3x3 grid.

Hi thanks for this response. But I couldn't hardcode the widget's
fixed size because I didn't know how the platform/style would paint a
radiobutton by default (i.e. what its height would be). I then had the
idea of trying sizeHint(). I also took some hints by manually
designing my desired appearance and compiling it using uic. What I did
was this (in Python):

refPtWidget = QWidget ( window )
refPtGrid = QGridLayout ( refPtWidget )
refPtGrid . setContentsMargins ( 0, 0, 0, 0 )
refPtArray = []
for i in range ( 3 ) :
	rowArray = []
	for j in range ( 3 ) :
		button = QRadioButton ()
		rowArray += [ button ]
		refPtGrid . addWidget ( button, i, j )
	refPtArray += [ rowArray ]
radioWidth = refPtArray [ 0 ] [ 0 ] . sizeHint () . width ()
refPtWidget . setFixedSize ( radioWidth * 3 - 10, radioWidth * 3 - 10 )

Actually the last line should read radioWidth * 3 + 1 if I am to go by
uic's default output but that's too much spacing for my
taste/requirement and so I reduced this. I realize though that it is
possible this can cause some clipping depending on the margin that the
sizeHint of the radiobutton provides around the actual painted area.
If one were to desire to totally avoid clipping they should use
radioWidth * 3 + 1.

-- 
Shriramana Sharma



More information about the Interest mailing list