How to set default height on QTableWidget - qt

I have a widget which I'm putting in a QVBoxLayout. This widget's layout is a QVBoxLayout which includes a QTableWidget. When I display this everything is fine but the QTableWidget only shows a few rows. How can I set the height of the table to a decent value (like 20 rows) while still allowing the table to resize?
I've tried calling table.setMinimumHeight(200) but then the table can NEVER be smaller than 200. I've also tried setting the container widget height using setMinimumHeight but this has the same problem.

Check the sizePolicy for the QTableWidget. This will have horizontal and vertical size policies which, if I understand you correctly, should be set to "expanding" or "minimum expanding". There are a number of options and combinations for these and sometimes it can be tricky getting the right combination for all the widgets in your layout to get what you are looking for.
The size policies will work in combination with your min/max height/width settings and those of the other widgets in the layout.

Related

How to resize QDockWidget

I have created one application without centralwidget in my mainwindows, and shown as maximized:
setCentralWidget(0);
setWindowState(Qt::WindowMaximized);
In my mainwindow, I have three docks. The dock1 is set to be docked to leftDockWidgetArea, the dock2 and dock3 are set to be docked to the rightDockWidgetArea. Also, I arrange dock2 and dock3 by
splitDockWidget(ui->dock2, ui->dock3, Qt::Horizontal);
So I got a results that the ratio of the width of the three docks are 2:1:1.
My question is how could I set the width of the three docks so that they can be arranged as 1:4:1.
Suppose my screen's width is 1024. I have tried
ui->dockWidget_PrimeVis->resize(1000, 1000);
This does not work since I found some people say the size of docks are controlled by mainwindow. So I found I could call resizeDocks in mainwindow such as
resizeDocks({ ui->dock1, ui->dock2,ui->dock3}, { 150,600,150 }, Qt::Horizontal);
And it does not work. Also, since each dock contains one subclassed widget, I also tried to set the size to each subclassed widget, again, does not work.
Since we can manually change the size of each dock by GUI, so I believe there should be a way to set the size of each dock with codes, right? Anyone has a clue?
The official documentation states the following:
A QDockWidget acts as a wrapper for its child widget, set with setWidget(). Custom size hints, minimum and maximum sizes and size policies should be implemented in the child widget. QDockWidget will respect them, adjusting its own constraints to include the frame and title. Size constraints should not be set on the QDockWidgetitself, because they change depending on whether it is docked; a docked QDockWidget has no frame and a smaller title bar.
Solution:
Just subclass the widget you are showing inside of the dockwidget. You can than overload different methods to change the size hint. For example: QWidget.minimumSizeHint()
Not Tested:
You could also just try to set the minimum size. But i do not know if this works.

How can I set a QPushButton to grow vertically?

Background: I need Command Link-like controls. Normally, I would use Qt's builtin QCommandLinkButton, but in this case I need to also support Right-to-Left layouts. Regrettably, QCommandLinkButton doesn't seem to respect Qt::RightToLeft when set explicitly via setLayoutDirection. There also doesn't seem to be a way to override the layout direction via style sheets.
I tried to work around this issue by using QPushButton: I instantiated QPushButton and replaced its contents with a custom layout containing two labels stacked vertically. This did not work because QPushButton refused to expand horizontally.
Failing to make QPushButton instances work, I decided to try sub-classing it. No matter what I tried (overriding sizeHint, setting vertical sizePolicy to expanding, resizeing explicitly, etc) the button refuses to grow more than 30 pixels high.
If I change QPushButton to QWidget, the new widget grows normally.
The question: How can I force a QPushButton to grow vertically and fit its inner widgets? If this is not possible, what are my options for a button-like widget that is both visually and behaviorally consistent with QPushButton and other "native" controls?
I managed to get the button to grow normally by setting the maximum height to 16777214 (instead of the default 16777215), and then setting the content margins of the button to 10.
Perplexingly, the default maximum height of a QPushButton should be plenty, but for some reason it refuses to grow past 30 pixels unless the maximum height is explicitly set to something else.
Place the button inside a layout and set its vertical size policy to Maximum.

How to restrict growth (size) of layout in Qt Designer?

When I maximize my window, I want to restrict a vertical layout (and the entire row below it also) so that it has a specific size (lets say a width of 200). How can I achieve this? Below is what I get now. The buttons are stretched too far. I want them to keep a width of 200.
To stop the buttons stretching, use the following steps in Qt Designer:
click on scrollArea in the Object Inspector
click on Break Layout on the toolbar
click on scrollArea in the Object Inspector
click on Lay Out in a Grid on the toolbar
click on scrollAreaWidgetContents in the Object Inspector
scroll down to the bottom of the Property Editor
change layoutColumnStretch to 0,1
These steps should remove an empty column from the scroll-area grid-layout, and make the second column stretch to take up the available space when the window is resized.
You just need to restrict the maximum width of all widgets (in this case the buttons) within the layouts of this grid column to the expected size, else they'll just keep expanding. You may also have to fiddle the horizontal size policy; I seem to remember that buttons were a bit tricky in this regard (or was that the height?), but can't test it right now.
The layout size contraint you tried only applies to the layout's direct parent widget, if it has one, which isn't the case for the vertical layouts here.

QSizePolicy true meaning, documentation and thoughts

I have been now two years working deeply with Qt layout system. After this time i encountered thosand of problems with SizePolicies and Layouts. I normally found out solutions, but never really understood what i was doing.
Now i was taking some time to try to understand correctly and build a 101 GUIDE for them and never fail. I found out this piece of information in the documentation about QSizePolicy of a QWidget:
This property holds the default layout behavior of the widget
If there is a QLayout that manages this widget's children, the size
policy specified by that layout is used. If there is no such QLayout,
the result of this function is used.
I thought that if you had a QLabel, for example, and you set the policies to Horizontal Expanding, Vertical Fixed, THE LABEL itself changed that way.
But it doesn't, at all.
When reading that i see that it talks about ITS CHILDREN inside the LAYOUT. So what does it mean, then?. Nothing is inside the label, is that why it does not work?.
WHat about inserting a label inside a QFrame, and telling the frame to be Expanding... Will the QFrame expand or stretch (depending on the rest of brother widgets in the same Layout) or will the label expand or stretch, not the QFrame?
What a mess...
What about Stretching?. If you set stretching 10 when you add the widget:
layout->addwidget(label, 10, Qt::AlignHCenter);
It doesn't work either.
Stretch 0 when you add the widget means : Take the policies i told you. Default, depends on the type of widget. Button-like widgets have expanding-fixed. Box-like Expanding-Expanding...
Stretch 10 means: grow maximum.
Am i right? Well. When having a QFrame and a QLabel inside, setting Expanding, and 10 to strech to the label DOES NOT WORK.
I don't understand all of your questions, so I will only answer to those that I think i do.
I thought that if you had a QLabel, for example, and you set the policies to Horizontal Expanding, Vertical Fixed, THE LABEL itself
changed that way. But it doesn't, at all.
I don't know what that means. What is the exact behavior you're expecting and what is actually happening? Is this label in a layout? Are there any other widgets in this layout?
WHat about inserting a label inside a QFrame, and telling the frame to be Expanding...
If you set a QSizePolicy to your QFrame object and set a layout to it, this size policy might be ignored. Docs: If there is a QLayout that manages this widget's children, the size policy specified by that layout is used. If there is no such QLayout, the result of this function is used. This means that the widget's layout should manage the size of the widget. Note that it will still respect the minimum/maximum width/height values.
Stretch 10 means: grow maximum.
What makes you think that?. The stretch factor is dependent on other widgets inside the layout. Docs: Stretch factors are used to change how much space widgets are given in proportion to one another.
When having a QFrame and a QLabel inside, setting Expanding, and 10 to strech to the label DOES NOT WORK.
Does not work how? If you set stretch for QFrame to 1 and for QLabel to 10, your QLabel object should always be 10 times as wide/high(depending of your layout type) as your QFrame object. If your QFrame has a layout and it contains children, then this might not work as it would depend on the childrens size policies.

Qt layout not expanding

I have a Qt Widget which has a frame, containg the rest of the widgets.
I only want one of the widgets to expand on maximazing the window, so I've set a max value for the others.
But it will only expand if I set a grid layout to the frame, which messes up the place of the widgets.
How can I solve it?
Do you use any kind of layout ?
Size policy only works if the parent has a layout set.
You can use a combination of different layouts to obtain the desired look, otherwise you will have to calculate the size of the 'expanding widget' yourself in the resize event method of the parent.

Resources