I wrote a custom widget which groups other widgets under expandable categories. Each category is denoted by a 'handle' widget which can be clicked or activated with the 'space' key in order to expand/collapse the associated widget. Handles are implemented in a class derived from QWidget with the following custom paintEvent():
Qt Code:
  1. void CWidgetHandle::paintEvent( QPaintEvent *event )
  2. {
  3. QPainter painter( this );
  4. QImage *icon = ( m_target->isVisible() ? &m_icnCollapse : &m_icnExpand );
  5.  
  6. painter.eraseRect( event->rect() );
  7. QSize ts = painter.fontMetrics().size( Qt::TextShowMnemonic, m_label, 0, NULL );
  8. painter.drawImage( 7, ( size().height() - icon->size().height() ) / 2, *icon );
  9. painter.drawText( QRect( QPoint( icon->size().width() + 10, ( size().height() - ts.height() ) / 2 ), ts ), m_label );
  10.  
  11. if ( hasFocus() ) {
  12. QVector<qreal> dashes;
  13. QPen pen;
  14.  
  15. dashes << 1 << 2 << 1 << 2 << 1 << 2 << 1 << 2 << 1 << 2;
  16.  
  17. pen.setColor( painter.pen().color() );
  18. pen.setStyle( Qt::CustomDashLine );
  19. pen.setDashPattern( dashes );
  20. pen.setCapStyle( Qt::RoundCap );
  21. painter.setPen( pen );
  22. painter.drawRect( 2, 2, size().width() - 5, size().height() - 5 );
  23. }
  24. }
To copy to clipboard, switch view to plain text mode 
The question I have is about proper drawing in the case of focused handles. Obviously the code as it stands now will only generate good looking visuals in only some cases. For exemplification this is how the test application looks while running under Gnome:

Handle focused (with an 'embedded' QPushButton)
handle_selected.png

Button focused
button_selected.png

Is there any way I can get Qt to draw that theme-specific focus rectangle for my custom widget ?