Results 1 to 7 of 7

Thread: Can't get layout to work - QScrollArea with form layout in tab

  1. #1
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question Can't get layout to work - QScrollArea with form layout in tab

    I am trying to use a QScrollArea as a tab page, but for some reason I cannot get the child widgets to layout correctly using a QFormLayout.
    I am not using Qt Designer in this case as i am dynamically adding rows to the form layout.

    Here is the code. AbstractPropertyPage is the class that is added to the tab widget. For the purpose of this post, i have omitted all but the relevant code, but i have other subclasses of AbstractPropertyPage which add specific properties to the page.
    Qt Code:
    1. class AbstractPropertyPage : public QScrollArea{
    2. Q_OBJECT
    3. protected:
    4. QScrollArea* scrollArea;
    5. QWidget* scrollAreaContents;
    6. QFormLayout* formLayout;
    7.  
    8. public:
    9. AbstractPropertyPage(QWidget* parent = 0);
    10.  
    11. void addRow(String labelText, QWidget* widget) {
    12. formLayout->addRow(labelText, widget);
    13. }
    14. //...
    15. };
    16.  
    17. AbstractPropertyPage::AbstractPropertyPage(QWidget* parent) :
    18. QWidget(parent) {
    19. scrollAreaContents = new QWidget(this);
    20. formLayout = new QFormLayout(scrollAreaContents);
    21. formLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
    22. this->setWidget(scrollAreaContents);
    23. }
    24.  
    25. //...
    To copy to clipboard, switch view to plain text mode 

    The subclasses then add rows:
    Qt Code:
    1. somePropertyWidget = makeLineEdit();
    2. addRow(tr("Some Property"), somePropertyWidget);
    To copy to clipboard, switch view to plain text mode 

    These widgets are then added to the tab widget:
    Qt Code:
    1. void PropertiesWindow::createPages() {
    2. //...
    3. tabWidget->addTab(new SomePropertyPage());
    4. //...
    5. }
    To copy to clipboard, switch view to plain text mode 

    I have also tried different approaches, such as subclassing QWidget instead of QScrollArea and add a QScrollArea to the widget. The widget would have a layout (e.g. vertical layout) such that the scroll area would fill the entire widget.

    I tried it in Designer, laying it out how it should look. As i could not set a QScrollArea as the tab widget (it automatically put a QWidget in), i did the second approach. But the generated code looked identical to mine.

    I don't know what i am doing wrong.
    [Window Detective] - Windows UI spy utility
    [War Thunder]

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can't get layout to work - QScrollArea with form layout in tab

    Try setting the "widgetResizable" property of the scroll area to true.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't get layout to work - QScrollArea with form layout in tab

    That was the only thing i did not try, as it defaults to false which is what i want.
    Setting it to true seemed to work, that is - the widgets are laid out in the form layout and the scrollbar appears when needed. But there is a strange artefact in the top left which looks like a widget that does not have a layout.

  4. #4
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't get layout to work - QScrollArea with form layout in tab

    As you say, it seems like a text edit that is a child of the QTabWidget (or at least a child of the "Window" page widget), but has not actually been added to the layout. Try calling findChildren (http://doc.qt.nokia.com/4.6.2/qobject.html#findChildren) on the QTabWidget and list the children to try and figure out where that line edit comes from.

  5. #5
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't get layout to work - QScrollArea with form layout in tab

    Hmm, now i can't even get any widgets to show. All i get is a small white square in the upper left corner and a background grey everywhere else. And i am using the same code as i posted earlier (i can't seem to edit it, but there are some errors in it). I forget what i did to get what i posted in the screenshot, but it was the closest i ever got to what i want.
    Last edited by xtal256; 13th February 2011 at 10:08.
    [Window Detective] - Windows UI spy utility
    [War Thunder]

  6. #6
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't get layout to work - QScrollArea with form layout in tab

    Sorry, i forgot, it was the setWidgetResizable(true) bit that got the layout to look like the image.
    I will now try findChildren and see if i can find out what that lone widget is. And stefanadelbert is right, it is a line edit. I initially didn't think that because it didn't have the text cursor, but that is because these controls are read-only.

    I think i can solve this issue now, the widget appears to be added incorrectly to the layout by my code. Shouldn't be too hard to figure out why.

    The real question is why setWidgetResizable(true) was needed. Doesn't this force widgets to resize in favour of adding a scrollbar (what i want the opposite)?
    Last edited by xtal256; 13th February 2011 at 23:52.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can't get layout to work - QScrollArea with form layout in tab

    Quote Originally Posted by xtal256 View Post
    The real question is why setWidgetResizable(true) was needed.
    Because otherwise your widget has the size of the scrollarea (or a bit smaller actually) and never grows regardless of the fact that you keep pushing widgets into its layout (the scrollarea forbids the layout to resize the widget) and scrollbars never appear. You want widgetResizable to be true most of the time.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    xtal256 (16th February 2011)

Similar Threads

  1. Dynamic form layout - Ability to add new rows
    By FaradayCage in forum Newbie
    Replies: 1
    Last Post: 22nd July 2010, 17:23
  2. Replies: 10
    Last Post: 9th June 2010, 15:12
  3. Resizing dialog with form layout
    By elizabeth.h1 in forum Qt Programming
    Replies: 2
    Last Post: 30th August 2009, 20:58
  4. QScrollArea: clearing the layout of the inner widget
    By mattc in forum Qt Programming
    Replies: 1
    Last Post: 3rd August 2009, 09:09
  5. Suggestions for checkbox form layout
    By MrGarbage in forum Qt Tools
    Replies: 1
    Last Post: 26th July 2007, 00:07

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.