PDA

View Full Version : QScrollArea and layouts



jesse_mark
13th September 2013, 16:28
Hello all,

I would like if anyone can explain to me
why in the designer and when using the QscrollArea the scroll bar are not shown, but when i run it the widget looks smaller and the scrolls are shown.

what i have tried :
Fix the size (width and height)
Setting the size policy to fixed (Horizontal and vertical)
change the layout margin of the group-box, where i have the scrollArea placed, and set all of them to zero.
, but still the same happen.

I have :
A group-box with a grid layout
then I places a scroll area on it with a grid layout as well
then places my other widget buttons, labels and lineEdit

see attachment for a pic in designer and in run time.

Santosh Reddy
13th September 2013, 19:35
Can you post a sample ui file?

ChrisW67
13th September 2013, 20:52
It looks to me like the group box does not have a layout applied, so the QScrollArea is taking an arbitrary size and needing to show scroll bars.

jesse_mark
13th September 2013, 22:37
@Santosh Reddy below is a sample ui.

@ChrisW67 .. I do have a layout for the group-box, as I mentioned in my description the group-box has a grid layout.

by main problem is that the widget in the designer is not the same when i run it.

//mainwindow.ui


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>498</width>
<height>523</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_11">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Main</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>450</width>
<height>213</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox_2">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>5000</width>
<height>5000</height>
</size>
</property>
<property name="title">
<string>Test</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<property name="margin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QScrollArea" name="scrollArea_2">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>221</width>
<height>164</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lineEdit_2"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="lineEdit_3"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="lineEdit_4"/>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>498</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

ChrisW67
13th September 2013, 23:15
@ChrisW67 .. I do have a layout for the group-box, as I mentioned in my description the group-box has a grid layout.
In your post you say you have a layout applied. My observation on the evidence presented is that this may not actually be the case. If I had dollarpound for every time someone was mistaken about their layouts...

Now we have an actual UI file (not one that matches your screen shots though) it appears you do have a layout applied at all levels.


by main problem is that the widget in the designer is not the same when i run it.
The size of the space the QScrollArea has to work with is driven by the layout of its container. The presence of scroll bars is driven by the size of the QScrollArea content widget. These things are determined at run time and Designer's preview is only a close approximation because it cannot know what container you have put this widget in, or how the window manager might have sized a top-level widget. There only needs to be a pixel or two in it to make the difference.

If you do not want scroll bars then do not put the widget in a scroll area or ensure that the scroll area's minimum size is larger than the contained widget's fixed or preferred size.

jesse_mark
17th September 2013, 18:13
Thanks,
The fact that each QScrollArea has a ScrollArea widget content with it, which you need to set its Min size,max Size and size policy for it as well, is what was not aware off.