newbie question: how to set a fixed height of a set of Widgets
-
Now I can come back to my initial intent.
I needed more than one push button on the right of the bottom table.
If I start with only one button top aligned and add a second button, the horizontal layout
is in charge and I get this outputIt comes without saying that for getting the buttons vertically aligned, one on top of the other, I need to put them inside a vertical layout.
But once they are inside a vertical layout, they are automatically and proportionally vertical centered, even if I align
them on the topSo my initiall question was, how can I get this result with QT layouts?
Thank you!
-
@cdvo said in newbie question: how to set a fixed height of a set of Widgets:
So my initiall question was, how can I get this result with QT layouts?
Remove the whole Layout Alignment stuff and put a vertical spacer below your buttons.
Since you are mainly using the Designer, it's simply drag'n'drop.Or by code
QSpacerItem *vSpacer = new QSpacerItem(0,0, QSizePolicy::Minimum , QSizePolicy::Expanding); ui->verticalLayout->addItem(vSpacer);
-
@cdvo
Yeah, things starting going tricky ...You have to add a vertical spacer after the buttons, but a border appears around the table because you set it a maximum size. Looks like a dead end ...
A solution could be to set a layout strech of 3,1 to the vertical layout (central widget).
-
@cdvo
Save this with .ui extension and have a try.<?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>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout" stretch="4,1"> <item> <widget class="QTableView" name="tableView"/> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout"> <property name="spacing"> <number>-1</number> </property> <item> <widget class="QWidget" name="widget" native="true"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="minimumSize"> <size> <width>0</width> <height>100</height> </size> </property> <property name="maximumSize"> <size> <width>16777215</width> <height>100</height> </size> </property> <layout class="QHBoxLayout" name="horizontalLayout_2"> <property name="leftMargin"> <number>0</number> </property> <property name="topMargin"> <number>0</number> </property> <property name="rightMargin"> <number>0</number> </property> <property name="bottomMargin"> <number>0</number> </property> <item> <widget class="QTableView" name="tableView_2"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="minimumSize"> <size> <width>0</width> <height>0</height> </size> </property> <property name="maximumSize"> <size> <width>16777215</width> <height>10000</height> </size> </property> </widget> </item> <item> <layout class="QVBoxLayout" name="verticalLayout_2"> <property name="spacing"> <number>-1</number> </property> <property name="sizeConstraint"> <enum>QLayout::SetFixedSize</enum> </property> <property name="bottomMargin"> <number>0</number> </property> <item> <widget class="QPushButton" name="pushButton"> <property name="text"> <string>PushButton</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pushButton_2"> <property name="text"> <string>PushButton</string> </property> </widget> </item> <item> <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> </item> </layout> </widget> </item> </layout> </item> </layout> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>22</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui>
-
@cdvo said in newbie question: how to set a fixed height of a set of Widgets:
If I do that I got similar effect as in my second screenshot from the beginning of this thread.
I dont know what you did, but I can get your desired result easily by doing what I've written above.
Both button pushed to the top, and aligned with theQTableView
Result:
Edit:
Even with
QTableView
's vertical settings set to fixed, it works. -
@cdvo said in newbie question: how to set a fixed height of a set of Widgets:
The trick in your case is to wrap the bottom table and the column of buttons inside a QWidget instead of wrapping them only with the horizontal layout, isn'it?
Yes, because without this, only the table is size constrainted, not the buttons.
In fact you circonvent the normal behavior of layouts, so the need to fiddling around .
Actually, in cases like yours, it's more usual to use splitters instead.
-
I dont know what you did,
Exactly what you suggest to do.
but I can get your desired result easily by doing what I've written above.
Both button pushed to the top, and aligned with the QTableViewI know. After that, when you lay out at the end the entire central widget you will see the vertical layout (with buttons and vertical spacer) growing more than the fixed height I wanted.
Could you please share your ui?
Thanks for your help
-
In fact you circonvent the normal behavior of layouts, so the need to fiddling around .
My intention was to get my desired layout using only horizontal and vertical layouts, optionally with grid layouts,
and their properties.Yes, because without this, only the table is size constrainted, not the buttons.
My knowledge of Qt is still very poor, but why the vertical layout containing the buttons and the spacer doesn't honor
the layoutSizeConstraint property set to SetFixedSize but instead grows as in my second screenshot at the top
of this thread?Thanks
-