Results 1 to 5 of 5

Thread: GraphicsScene navigation widget

  1. #1
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default GraphicsScene navigation widget

    I was wondering if anyone here has implemented a custom GraphicsView with a navigation widget different from the ordinary scrollbars.

    Specifically I was thinking about the navigation widget used in GoogleEarth. It has some really nice benefits.
    • Completely intuitive
    • Looks pretty
    • Easier to use on a tablet where scrollbars might be difficult to hit with a stylus.

    I'll reverse-engineer one for my own purposes, if I must. I was just trying to save myself some work on that front by asking if anybody'd done anything similar already. I'm really attached to that last benefit.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: GraphicsScene navigation widget

    Since noone else answered this question I decided it's time to do it

    Based on the number of answers to this thread I think it is safe to assume such widget doesn't exist. Implementing it is not a problem, a completely different thing is how to use it. The way I see it is that it would have to be a separate widget placed on top of the view. It means you'd need to subclass the view and add the navigation widget and make sure it is always in a proper position regardless of the view's geometry.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsScene navigation widget

    I was just wondering if people have come across this . Guess when it will be available

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: GraphicsScene navigation widget

    It's already available, just probably hardly usable And completely useless in the case discussed in this thread.

  5. #5
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: GraphicsScene navigation widget

    I've already figured out how to do a widget overlay on a scroll area, when I was working on a compass rose for geospatial imagery.

    I was hoping, that failing to find a navigation prefabbed widget (which I expected), the discussion might morph into one about usability, my primary reason for even asking for the widget.

    So far I've only seen scrollbars and the GoogleEarth widget for geospatial stuff. Perhaps some gamers know of additional interesting means of navigating a large QGraphicsScene?

    Anyway, here was my solution for the compass rose. The trick was to have the rose paint on itself (with transparent background), and have the parent widget move it to an appropriate place. For QScrollArea this meant NOT painting on the viewport.

    Qt Code:
    1. /**
    2.  * @short A widget that displays a compass rose in the top right corner.
    3.  *
    4.  */
    5. class CompassRose : public QWidget
    6. {
    7. public:
    8. CompassRose( QWidget *parent );
    9.  
    10. protected:
    11. void paintEvent( QPaintEvent *pe );
    12.  
    13. private:
    14. qreal m_angle; //angle is degrees
    15. };
    To copy to clipboard, switch view to plain text mode 

    and the really interesting part
    Qt Code:
    1. void CompassRose::paintEvent( QPaintEvent *e )
    2. {
    3. QFont font = QFont("Sans Serif", 8);
    4. font.setStyleStrategy(QFont::ForceOutline);
    5.  
    6. int fontheight = QFontMetrics(font).ascent();
    7. int fontwidth = QFontMetrics(font).boundingRect("N").width();
    8.  
    9. QMatrix matrix;
    10. matrix.translate( width()/2, height()/2 );
    11.  
    12. QPainter p(this);
    13. p.setMatrix( matrix );
    14. p.setRenderHint(QPainter::Antialiasing, true);
    15.  
    16. // draw the surrounding ellipse
    17. p.setPen( QPen(Qt::black, 1) );
    18. p.setBrush(QColor(192,192,192,192));
    19.  
    20. int offset = qMax( fontheight, fontwidth ) +5;
    21. int size = qMin( width(), height() );
    22. int radius = (size-1)/2 - offset;
    23. p.drawEllipse( -radius, -radius, radius*2, radius*2 );
    24.  
    25. // draw the text
    26. qreal midx = (fontwidth/2+radius+4)*cos(-m_angle*DTOR);
    27. qreal midy = (fontheight/2+radius+4)*sin(-m_angle*DTOR);
    28. QRectF rect( midx-fontwidth/2, midy-fontheight/2, fontwidth, fontheight );
    29.  
    30. path.addText( QPointF(midx-fontwidth/2, midy+fontwidth/2), font, "N" );
    31.  
    32. p.setPen(QPen( QColor(192,192,192,192), 3 ));
    33. p.setBrush( Qt::black );
    34. p.drawPath( path );
    35. p.setPen( Qt::NoPen );
    36. p.drawPath( path );
    37.  
    38. // draw the compass
    39. matrix.rotate( -m_angle );
    40. p.setMatrix( matrix );
    41.  
    42. int outer = radius-5;
    43. int inner = radius/4;
    44.  
    45. static const QPointF polyblack[3] = {
    46. QPointF( outer, 0 ),
    47. QPointF( inner*cos(45.*DTOR), -inner*sin(45.*DTOR) ),
    48. QPointF( 0, 0 )
    49. };
    50. static const QPointF polywhite[3] = {
    51. QPointF( outer, 0 ),
    52. QPointF( inner*cos(45.*DTOR), inner*sin(45.*DTOR) ),
    53. QPointF( 0, 0 )
    54. };
    55.  
    56. p.setPen( Qt::NoPen );
    57. for (int i=0; i<4; ++i) {
    58. matrix.rotate( 90. );
    59. p.setMatrix(matrix);
    60.  
    61. p.setBrush( Qt::black );
    62. p.drawPolygon( polyblack, 3 );
    63.  
    64. p.setBrush( Qt::white );
    65. p.drawPolygon( polywhite, 3 );
    66. }
    67. }
    To copy to clipboard, switch view to plain text mode 

    Usage is something like so...
    Qt Code:
    1. ScrollArea::ScrollArea( QWidget *parent )
    2. :QScrollArea( parent )
    3. {
    4. d = new ScrollAreaPrivate();
    5. d->compass = new CompassRose( this );
    6.  
    7. setObjectName( "pss::ScrollArea" );
    8. setAttribute( Qt::WA_StaticContents );
    9.  
    10. //setWidget(new myWidget(this));
    11. viewport()->setFocusProxy( this );
    12. viewport()->setFocusPolicy( Qt::StrongFocus );
    13. //widget()->setAttribute( Qt::WA_OpaquePaintEvent );
    14. //widget()->setAttribute( Qt::WA_NoSystemBackground );
    15. //widget()->setMouseTracking( true );
    16. setWidgetResizable( true );
    17.  
    18. }
    19.  
    20. void ScrollArea::resizeEvent( QResizeEvent *re )
    21. {
    22. qDebug() << "sa resize event" << re;
    23. QScrollArea::resizeEvent(re);
    24. //d->compass->resize(viewport()->size());
    25. d->compass->move( viewport()->width() - d->compass->width(), 0 );
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by EricTheFruitbat; 2nd April 2007 at 22:12. Reason: emphasize a question

Similar Threads

  1. Controlling which widget on top layer?
    By JonathanForQT4 in forum Qt Programming
    Replies: 6
    Last Post: 22nd March 2007, 14:27
  2. Replies: 4
    Last Post: 10th March 2007, 18:01
  3. Pin/Unpin Dock Widget
    By charlesD in forum Newbie
    Replies: 1
    Last Post: 21st June 2006, 06:57
  4. Replies: 4
    Last Post: 24th March 2006, 22:50
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

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.