I have two widgets, one arbitrary (usually a QLineEdit), and one QLabel which displays a Pixmap. They are placed next to each other with a QHBoxLayout. The widget with this layout is in turn placed in another layout.
Now, what I want is that the label with the pixmap is automatically resized so that it is as high as the arbitrary widget next to it. However, even when I set the label's sizePolicy to Maximum, it still seems to expand to the original pixmap size, instead of resizing the pixmap and shrinking to match the other widget. Instead of having two equally large widgets I have the arbitrary one which is smaller than the pixmap next to it.
Any ideas how to get the size of the pixmap label to match the size of the widget next to it?
How about:
int height = arbitraryWidget->height(); // get desired height.
label->setSizeHint(QSize(label->width(), height); // set size hint to current width and desired height.
label->setSizePolicy(QSizePolicy::Fixed); // optional, but this ensures size is desired.
Related
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.
I have a small program which contains a QGroupBox with other widgets like this:
I tried many ways to manage the size of the QGroupBox to make the height as the same as the rest of the parts. Except for the way of using setMaximumHeight, because I want the size to change dynamically with the window size too. what else can I do to manage the layout?
Right now there are three items in a layout. The layout will try to fill the available space. QLineEdit and QSpinBox (or whatever your second widget is) have SizePolicy.vertical == fixed, so all extra space goes to the QGroupBox.
You have these choices:
Add a vertical spacer as fourth item below the groupbox to your layout.
Set maximum height of your groupbox - then the remaining space will be evenly spaced between the items.
Adjust the size of your window / widget / dialog (in Qt Designer or via code).
For exemple, I want the value of the layoutLeftMargin property to be equal to 1/3 of the parent widget. So when I will resize the windows, the ratio of the widget will still stay the same.
Else, if it's not possible with QtDesigner, how can I do it with code ?
No, Margins are specified in pixels, they can't be relative to the parent widget's size.
However, You can do that in the designer by putting your whole current layout in a Horizontal Layout, add a Horizontal Spacer to the left of it, assign suitable layoutStretch values in the horizontal layout (In your example, this should be 1,2, meaning the original layout will take up twice the space taken up by the spacer, so that the spacer gets 1/3 of the parent widget).
I have an widget x with fixed size. Then at first I took a QScrollArea and then a QGridLayout which I set as the layout of scrollAreaWidgetContent. Then I started adding some widget x in the layout at (0,0), (0,1), (1,0), (1,1)... of grid layout. Then when I ran the program, strangely when I shrink the window vertically, the widgets overlap.
And as expected, when I increase the window size vertically, they are not overlapped anymore.
But strange thing is this problem does not occurring when I shrink window horizontally. For example,
My question is, why this is happening and more importantly, keeping in mind I want to use gridlayout, how to solve this problem?
As you said in your question:
Your widget has a fixed size, so the layout does not shrink them when there is no space left.
So the question is: what do you want to happen when you shrink the window?
If you want to shrink your widget you have to change the sizePolicy of your widgets from "Fixed" to "Preferred".
If you want to reduce the number of widgets in the layout, then you could add an event handler in the resize event and remove them
If you want to disallow the shrinking of the window, then you need to set the sizePolicy of the scrollarea to a fixed or minimumSize
I have a QTableWidget, QTextEdit, and cancel & okay buttons.
I want these widgets to stay in the same position relative to each other, and the QTableWidget to expand if the dialog window is expanded or size changed...
How can I do that?
You need to look into Qt's layout system - using layouts will handle automatically resizing your objects based on the size of their parent.
A combination of using QWidget::setSizePolicy() and QBoxLayout::setStretch() (or more likely QBoxLayout::insertWidget(..., int stretch = 0, ...)) will allow you to acheive the behaviour you refer to where only certain objects expand to fill available space, while others remain a constant size.
Addressing the image you've given above as an example:
Aside from dragging and dropping objects into the form, to achieve this solution I have:
Set the vertical sizePolicy of textEdit to Fixed.
Set a height in textEdit's minimumSize for the sizePolicy to use.
Set layoutStretch in centralWidget to 1,0, i.e. assign the minimum possible space for the elements contained in horizontalLayout and give any remaining space to tableWidget.