Results 1 to 1 of 1

Thread: Margins around child widget of a QScrollArea?

  1. #1
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Margins around child widget of a QScrollArea?

    Hi,

    Is there a simple and robust way to add margins around the child widget of a QScrollArea in Qt 4.7? Something like scrollarea->widget()->setContentsMargins(20) would have been nice but such a functionality seems to be absent in Qt.

    Here's what I've tried: I created a second widget with a QGridLayout into it to act as a wrapper around the first widget. The QGridLayout has the desired margins set with QGridLayout::setContentsMargins(). I also set the size constraint on the layout to QLayout::SetFixedSize:

    Qt Code:
    1. // Create a wrapping QWidget with margins.
    2. QWidget* widget_wrapper = new QWidget();
    3. widget_wrapper->setLayout(new QGridLayout());
    4. widget_wrapper->layout()->setSizeConstraint(QLayout::SetFixedSize);
    5. widget_wrapper->layout()->setContentsMargins(20, 20, 20, 20);
    6. widget_wrapper->layout()->addWidget(widget);
    7.  
    8. // Create the scroll area and attach the wrapping QWidget to it.
    9. QScrollArea* scroll_area = new QScrollArea();
    10. scroll_area->setAlignment(Qt::AlignCenter);
    11. scroll_area->setWidget(widget_wrapper);
    To copy to clipboard, switch view to plain text mode 
    This almost works, but not quite. One situation where it doesn't work properly is when the user wants to zoom on the inner widget: to make that happen, I resize the inner widget using QWidget::setFixedSize().

    Can this approach work transparently? Am I using the right size constraint on the QGridLayout? Are there other, simpler approaches?

    Thanks for all the input.

    Cheers,
    Franz


    Added after 1 hour 55 minutes:


    Problem solved. I needed to force a layout update (scroll_area->widget()->layout()->activate()) after having resized the inner widget in order to ensure that the scroll area child widget is resized as well.
    Last edited by dictoon; 21st April 2011 at 17:59.

Similar Threads

  1. Replies: 1
    Last Post: 11th March 2011, 19:34
  2. Widget Margins
    By qtoptus in forum Qt Programming
    Replies: 8
    Last Post: 24th July 2010, 01:29
  3. Replies: 0
    Last Post: 19th April 2010, 16:29
  4. Replies: 7
    Last Post: 14th January 2010, 08:47
  5. QScrollArea margins
    By munna in forum Qt Programming
    Replies: 12
    Last Post: 18th September 2006, 18:56

Tags for this Thread

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.