Results 1 to 2 of 2

Thread: Using radio button for checkable QGroupBox

  1. #1
    Join Date
    Feb 2008
    Qt products
    Qt4 Qt/Embedded
    Unix/X11 Windows

    Default Using radio button for checkable QGroupBox

    Hello, does anyone know a quick way to have the indicator subcontrol of a QGroupBox appear as a radio button instead of a checkbox?

    I would like the solution to work for all styles (e.g. using a stylesheet to provide a radio button image would not work as I would have to provide a different image depending on whether the application was using windows style, mac style etc.)

    I assume that I'm going to have to reimplement the paint event for the subcontrol, but was wondering if there is a simpler way?


  2. #2
    Join Date
    Feb 2008
    Qt products
    Qt4 Qt/Embedded
    Unix/X11 Windows

    Default Re: Using radio button for checkable QGroupBox

    It turns out that subclassing QGroupBox and re-implementing the paint event was easier than I thought. I didn't realise all the QStylePainter/QStyleOption stuff existed until now.

    I basically drew the group box, erased the checkbox, then drew a radio button. Does anyone have any comments as to why this might give me problems. (I'm wondering about the way I've erased the checkbox, possibly giving strange artifacts on some fancy style I don't know about).

    Re-implemented paintEvent():
    Qt Code:
    1. void TRadioGroupBox::paintEvent( QPaintEvent * )
    2. {
    3. QStylePainter paint( this );
    4. initStyleOption( &option );
    5. // don't remove the original check box control, as we want to keep
    6. // it as a placeholder
    7. // option.subControls &= ~QStyle::SC_GroupBoxCheckBox;
    8. paint.drawComplexControl( QStyle::CC_GroupBox, option );
    10. // re-use the style option, it contians enough info to make sure the
    11. // button is correctly checked
    12. option.rect = style()->subControlRect( QStyle::CC_GroupBox, &option,
    13. QStyle::SC_GroupBoxCheckBox, this );
    15. // now erase the checkbox
    17. QPixmap px( option.rect.width(), option.rect.height() );
    18. px.fill( this, option.rect.left(), );
    19. QBrush brush( px );
    20. paint.fillRect( option.rect, brush );
    21. paint.restore();
    23. // and replace it with a radio button
    24. paint.drawPrimitive( QStyle::PE_IndicatorRadioButton, option );
    25. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to access QGroupBox members?
    By gren15 in forum Newbie
    Replies: 2
    Last Post: 20th March 2009, 17:08
  2. Radio buttons in a tree view
    By notwithstanding in forum Qt Programming
    Replies: 6
    Last Post: 3rd November 2008, 22:32
  3. Coloring of Radio Button
    By jogeshwarakundi in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 22nd July 2008, 15:05
  4. Undoing radio button checks.
    By Doug Broadwell in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2007, 09:12
  5. Paint XP radio button to pixmap
    By Ben.Hines in forum Qt Programming
    Replies: 2
    Last Post: 26th April 2006, 21:15


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.