Results 1 to 3 of 3

Thread: Fit to width in QScrollArea unstable

  1. #1
    Join Date
    May 2006
    Location
    Walchwil
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Red face Fit to width in QScrollArea unstable

    I have a nasty problem with QScrollArea.

    I draw a document where the width / height ratio is constant. The document has a zoom factor.

    I use in widget() the function minimumSizeHint() which give back conforming of the document zoom factor and the parentWidget()->size() the new document size. Because of the fit to width the document size is the same than the parent widget size.

    Ok until now all seems ok!

    But, if the widget size is too big the document will switch on the vertical scrollbar. But the new widget size (with the vertical scroller) could draw a smaller document which one is enough small to fit in the widget, now the vertical scroll will switch off ... and so on ...

    Any one has an idea how to do it ?

    Bilbon.
    Last edited by Bilbon; 11th October 2006 at 13:13.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Fit to width in QScrollArea unstable

    Have you tried QScrollArea::setWidgetResizable(true)? It will resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space. I haven't looked into the sources but I believe QScrollArea in "widget resizable" mode honors it's widget's minimum size hint and the scroll bars appear when the size would be less than that.
    J-P Nurmi

  3. #3
    Join Date
    May 2006
    Location
    Walchwil
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Fit to width in QScrollArea unstable

    It's exactly the problem ... I have set QScrollArea::setWidgetResizable(true)!!

    But you have 2 states: one where you need a scroller (because it's too big to fit in) and one where you have a scroller but you don't need one (because it's enough small).

    1 resize event -> 2 paint event -> 1 resize event -> 2 paint event ....

    All works fine in scroll area if you have a fixed size of widget(), but if your size denpends of the scroll area size.

    Ok, after 2 days of fight I have found a solution (not so beautiful ) in a derivation of QScrollArea with margins .... during the resize event:

    Qt Code:
    1. int scroll_pixel = style()->pixelMetric(QStyle::PM_ScrollBarExtent);
    2. QSize size_pixel = size() - 2*QSize(frameWidth(), frameWidth());
    3.  
    4. qreal zoom_min = size_pixel.width() / doc_size_pixel.width();
    5. qreal zoom_max = (size_pixel.width() - scroll_pixel) / doc_size_pixel.width();
    6.  
    7. bool need_a_margin = (zoom_min * doc_size_pixel.height() > size_pixel.height());
    8. bool need_a_scroll = (zoom_max * doc_size_pixel.height() > size_pixel.height());
    9.  
    10. m_zoom = (need_a_margin) ? zoom_max : zoom_min;
    11.  
    12. setContentsMargins(0, 0, (need_a_margin && !need_a_scroll) ? scroll_pixel : 0, 0);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Bilbon; 12th October 2006 at 11:03.

Similar Threads

  1. Replies: 2
    Last Post: 8th October 2006, 20:14
  2. Replies: 7
    Last Post: 20th September 2006, 14:45
  3. is there any signal for QScrollArea?
    By rajesh in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2006, 07:12
  4. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 22:59
  5. How to obtain the width of a QTableWidget?
    By Giel Peters in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2006, 22:34

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.