Results 1 to 5 of 5

Thread: QGraphicsView Horizontal Stretch in a QSplitter

  1. #1
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsView Horizontal Stretch in a QSplitter

    I am designing a simple GUI that includes a QSplitter containing a QListWidget and a QGraphicsView coupled horizontally. I want the list widget on the left side occupying 20% of the splitter container and the graphics view on the right side occupying 80% of the splitter. I want the user to be able to adjust these ratios by dragging the splitter bar, so I cannot fix these values. Here is approximately how I want it to look:
    Capture1.PNG

    It actually works fine when I set the horizontal stretches to 20 and 80 (as seen in the above picture) until I set the graphics view scene with QGraphicsView::setScene(). Then the splitter bar moves over to the right side of the window like this:
    Capture2.PNG

    It seems to give the QGraphicsView widget the minimum amount of space allowed in the splitter. I tried setting the horizontal stretch again after setting the scene like this:
    Qt Code:
    1. ui.graphicsView->setScene(scene);
    2. ui.graphicsView->sizePolicy().setHorizontalStretch(80);
    To copy to clipboard, switch view to plain text mode 
    but it does not seem to make a difference. How can I avoid this resizing when setting the scene? Thanks.

  2. #2
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView Horizontal Stretch in a QSplitter

    I resolved the problem.

    I tried setting everything up in code instead of using Qt Designer and it worked as expected. Finally, I realized that I must have done something wrong in Qt Designer and went back to check. Turns out that Designer hides away the central widget in the window layout, so I had actually been putting the layout on the window the whole time instead of the central widget. I had to select the central widget in the tree view on the side and then set the layout from the top bar menu (instead of context menu). Now it all seems to work

  3. #3
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView Horizontal Stretch in a QSplitter

    I actually did not resolve my problem. It looks like I misunderstood my own solution. I had accidentally removed the splitter (which then does not allow the user to resize). After looking into a little more, the problem seems to be a known issue when splitters and layouts interact. Here is some relevant information for anyone else who runs into the same problem:

    http://www.qtcentre.org/threads/4440...ch-has-control

  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView Horizontal Stretch in a QSplitter

    Couple of things:

    1) Code you've posted at the beggining is just wrong, you change stretch factor on a copy of the size policy! you have to do it this way:
    Qt Code:
    1. QSizePolicy policy = widget->sizePolicy();
    2. policy.setHorizontalStretch(stretch);
    3. policy.setVerticalStretch(stretch);
    4. widget->setSizePolicy(policy);
    To copy to clipboard, switch view to plain text mode 

    2) stretch factor is probably not entirely what you think, from docs:
    void QSplitter::setStretchFactor ( int index, int stretch )
    Updates the size policy of the widget at position index to have a stretch factor of stretch.

    stretch is not the effective stretch factor; the effective stretch factor is calculated by taking the initial size of the widget and multiplying it with stretch.
    So setting factors of 20 and 80 will not result in 20%/80% split.

    3) Minimum size of graphics view depends on the scene rectangle. If you don't set any then the view will be always squizes to smallest possible regardless of stretch factor.
    Qt Code:
    1. QGraphicsScene* scene = new QGraphicsScene( this );
    2. scene->setSceneRect( 0, 0, 500, 500 ); // this is what you need
    3. QListWidget* list = new QListWidget();
    4.  
    5. view->setScene( scene );
    6. scene->addEllipse( 50, 50, 40, 40, QPen( Qt::red ), QBrush( Qt::blue ) );
    7.  
    8. QSplitter* sp = new QSplitter( this );
    9. sp->addWidget( list );
    10. sp->addWidget( view );
    11.  
    12. sp->setStretchFactor( 0, 1 );
    13. sp->setStretchFactor( 1, 2 );
    14.  
    15. this->setCentralWidget( sp );
    To copy to clipboard, switch view to plain text mode 
    Code above creates a spliter with list and view in (more or less) 20%/80% initial split. Changing scene rect will require chaning stretch factor to keep 20/80 ratio.
    BTW setSize() doesn't work when it comes to setting initial widgets sizes. Never figured it out why though.

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

    bassPenguin (12th April 2012)

  6. #5
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView Horizontal Stretch in a QSplitter

    Thanks Spitfire! Good catch on the code. It took me too long to see that mistake . Thanks for the clarification on QSplitter::setStretchFactor(). I did actually get what I was looking for using QSplitter::setSizes(), but I have only tested it in Windows. If I have trouble later, I will try setting the scene rectangle as you suggested.

Similar Threads

  1. [QGraphicsView] Size stretch
    By bspitz in forum Qt Programming
    Replies: 4
    Last Post: 10th May 2011, 09:23
  2. Stretch Last Section
    By skizzik in forum Qt Programming
    Replies: 1
    Last Post: 10th January 2011, 14:45
  3. Can Layouts Stretch to Fit?
    By kylemhall in forum Newbie
    Replies: 2
    Last Post: 17th December 2009, 15:37
  4. Resize event on stretch?
    By kodiak in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2008, 22:51
  5. stretch QTableWodgetItem
    By Shawn in forum Qt Programming
    Replies: 5
    Last Post: 5th September 2007, 09:49

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.