Results 1 to 5 of 5

Thread: QGraphicsView Horizontal Stretch in a QSplitter

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

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

    bassPenguin (12th April 2012)

  3. #2
    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
  •  
Qt is a trademark of The Qt Company.