Results 1 to 2 of 2

Thread: [Qt4/X11] Drawing theme-specific focus marker (rectangle) on custom widget

  1. #1
    Join Date
    Mar 2009
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [Qt4/X11] Drawing theme-specific focus marker (rectangle) on custom widget

    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 ?

  2. #2
    Join Date
    Mar 2009
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4/X11] Drawing theme-specific focus marker (rectangle) on custom widget

    Since my last post I have found a (partial) solution to the problem. I've modified the paintEvent() posted above to this:
    Qt Code:
    1. void CWidgetHandle::paintEvent( QPaintEvent *event )
    2. {
    3. QStylePainter 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.  
    13. bopt.initFrom( this );
    14. bopt.rect = QRect( QPoint( 2, 2 ), QSize( size().width() - 4, size().height() - 4 ) );
    15.  
    16. painter.drawPrimitive( QStyle::PE_FrameFocusRect, bopt );
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    This now generates the expected output under Gnome but under Kde nothing (as in no focus frame) is displayed. If anyone has any suggestion don't be shy
    Last edited by gargoylle_ltk; 4th November 2009 at 00:02.

Bookmarks

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.