Results 1 to 14 of 14

Thread: scrollbars and qgraphicsview

  1. #1
    Join Date
    Feb 2009
    Posts
    18
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default scrollbars and qgraphicsview

    I have a problem with setting the maximumsize of a qgraphicsview.

    I do not want any background canvas area to be visible in the qgraphicsview, so when the window is sized sufficiently large, it should be as in below picture (except without scrollbars.)

    For smaller window sizes, the view can be smaller and scrollbars are required.

    The problem is, I cannot get the scrollbars to disappear for a maxed window, where the maximum qgraphicsview size is limited to the views bounding rectangle:



    Qt Code:
    1. MyMain::MyMain(QWidget *parent):QMainWindow(parent)
    2. {
    3. QIcon OpenIcon = this->style()->standardIcon(QStyle::SP_DirOpenIcon, 0, this);
    4.  
    5. QAction *quit = new QAction("&Quit", this);
    6. QAction *open = new QAction("&Open", this);
    7.  
    8. QMenu *file = this->menuBar()->addMenu("&File");
    9. file->addAction(open);
    10. file->addAction(quit);
    11.  
    12. QToolBar *toolbar = addToolBar("main toolbar");
    13. QAction *open2 = toolbar->addAction(OpenIcon, "Open File");
    14. toolbar->addSeparator();
    15.  
    16. scene = new QGraphicsScene();
    17. scene->addText("Hello, world!",QFont("Helvetica [Cronyx]", 100));
    18. view = new QGraphicsView(scene, this);
    19. scene->itemsBoundingRect();
    20.  
    21. view->setContentsMargins ( 0,0,0,0 );
    22. view->setMaximumSize(view->sceneRect().width()+1,view->sceneRect().height()+1);
    23. //view->setMaximumSize(view->maximumViewportSize()); /* also does not work*/
    24.  
    25. this->setCentralWidget(view);
    26. this->resize(800,300);
    27. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: scrollbars and qgraphicsview

    Hi,

    Take a look at QAbstractScrollArea(note that QGraphicsView inherits it):

    setVerticalScrollBarPolicy
    setHorizontalScrollBarPolicy

    You can set them to "Qt::ScrollBarAlwaysOff" or "Qt::ScrollBarAlwaysOn". So you have to know when you want to show or hide them.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Feb 2009
    Posts
    18
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: scrollbars and qgraphicsview

    Thanks ^NyAw^ .

    I indeed checked this out, however the default is a mode where it hides the unneeded scrollbars. I only wonder why this command does not function:

    view->setMaximumSize(view->sceneRect().width()+1,view->sceneRect().height()+1);

    because if I add a margin of say 30 pixels, it will autohide unneeded scrollbars. But the graphicsview is too large in that case (I want the view showing a pixmap, without anything else).

    I wonder which borders I'm missing here, or something else I'm sizing wrong?

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: scrollbars and qgraphicsview

    Hi,

    I think that the scrollBars appear because the scene is larger than the view, so you are defining the maximum size of the view but the scene is larger so the scrollBars apear.
    Try resizing the scene to the view size.
    Òscar Llarch i Galán

  5. The following user says thank you to ^NyAw^ for this useful post:

    captiva (20th February 2009)

  6. #5
    Join Date
    Feb 2009
    Posts
    18
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: scrollbars and qgraphicsview

    Ok, I tried that, in the docs of qgraphicsview:

    If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).


    So I added:

    Qt Code:
    1. view->setSceneRect(QRect());
    To copy to clipboard, switch view to plain text mode 

    But unfortunately, to no avail...

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: scrollbars and qgraphicsview

    Hi,

    may be a stupid question, but you are setting a maximum size which does not mean that the size of the view is set to it. So to be sure also call

    Qt Code:
    1. view->setContentsMargins ( 0,0,0,0 );
    2. view->setMaximumSize(view->sceneRect().width()+1,view->sceneRect().height()+1);
    3. view->resize(view->maximumSize());
    To copy to clipboard, switch view to plain text mode 

    Lykurg

  8. The following user says thank you to Lykurg for this useful post:

    captiva (20th February 2009)

  9. #7
    Join Date
    Feb 2009
    Posts
    18
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: scrollbars and qgraphicsview

    Ok, just tried that as well. Unfortunately it doesn't work.

    I think the size is sufficiently large, because i set the maximum size of the widget to be 1 pixel larger than the view. And the view has the same size as the scene.

  10. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: scrollbars and qgraphicsview

    Ha, one last thought, sometime the fonts are buggy related to their bounding boxes. Did you try different fonts with the same result?

    Lykurg

  11. #9
    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: scrollbars and qgraphicsview

    You can try calling QGraphicsView::fitInView based on you main window size. This will ensure all contents of scene will be visible.

  12. The following user says thank you to aamer4yu for this useful post:

    captiva (20th February 2009)

  13. #10
    Join Date
    Feb 2009
    Posts
    18
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: scrollbars and qgraphicsview

    Thanks, also tried that, unfortunately it also does not work.

    I gave up on the graphicsview, instead trying to modify the qt image viewer example and there I see the same problem. If i set maximumsize, scrollbars appear where they should not !

    Does anyone have an example of a qscrollarea or qgraphicsview in a window, where the window cannot be stretched further AND the scrollbars disappear, if the view is filled completely. The layout management of qt is some kind of voodoo to me.

    Im now starting to become completely desperate, this cannot be impossible???
    Last edited by captiva; 19th February 2009 at 10:43.

  14. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: scrollbars and qgraphicsview

    EDIT: forget it...

  15. #12
    Join Date
    Feb 2009
    Posts
    18
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: scrollbars and qgraphicsview

    This must be implemented in many qt programs, nobody with a hint on how to tackle this?

  16. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: scrollbars and qgraphicsview

    Äh, sorry, my comment was capable of being misunderstood. I meant forget my post, not your issue...

  17. #14
    Join Date
    Feb 2009
    Posts
    18
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Red face Re: scrollbars and qgraphicsview

    Ah, I figured out the scrollbar problem at least:

    I set a maximum size on the QScrollArea / QGraphicsView widget, but that size should include the widget borders (which are exactly 2 px). When I add exactly 4 pix to the maximumsize (2px at each side) the scrollarea is exactly the right size, without scrollbars.

    This is probably due to the fact that the widget inherits from QFrame, the frame is removable by setting the frameShape of the widget to NoFrame. (currently testing) Better would be some way to query the border size, or to set the size within the borders, but I don't know how to do that.

    Thanks for all the help!

Similar Threads

  1. scrollbars in MaximumSize QGraphicsView
    By captiva in forum Newbie
    Replies: 0
    Last Post: 17th February 2009, 09:24
  2. QGraphicsView, unmanaged artifact with scrollbars
    By philw in forum Qt Programming
    Replies: 1
    Last Post: 26th December 2008, 02:58
  3. QGraphicsView and scrollbars
    By godmodder in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2008, 22:34
  4. Problem determining size of QGraphicsView
    By Bocki in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2008, 14:54
  5. QEvent for QGraphicsView scrollbars?
    By forrestfsu in forum Qt Programming
    Replies: 9
    Last Post: 2nd December 2006, 07:42

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.