I am now trying to resize a qgraphicsview to a preffered size:
I am using layouts:
this->layout()->addWidget(&qtview);
I have layouts everywhere, so thet the size of my main widow is adjusted depending on its contents.
If I use:
qtview.setMinimumWidth(500);
qtviewSetMinimumHeight(500);
Then my view is scaled to the given size, but I am not able to shrink the main window. (which makes sense as I set a minimum soze)
I would like to tell qt my preferred (not minimum or maximum) size for the view, letting the view use scrollbars when needed (i.e. if the preferred size does not fit in my screen), or letting qt have a smaller view than my preferred size if the scene is smaller than my view, (or enlarge the scene to fit the view in this case)
I am searching for something looking like an hypothetical method setPreferredWidth().
What can I do to get this behaviour?
It sounds like you're looking for the QGraphicsView function setSizePolicy: -
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
Related
I'm trying to follow the example at the below link to have a picture (in a qlabel) shown in a scrollable area.
https://doc.qt.io/qt-5/qtwidgets-widgets-imageviewer-example.html
I'm using Qt Designer to make the ui instead of hardcoding everything. So I have a QLabel, in a QWidget (with a grid layout assigned to it), in a QScrollArea.
From the tutorial, they state the following for the sizepolicy of the QLabel:
We set imageLabel's [QLabel] size policy to ignored, making the users able to scale the image to whatever size they want when the Fit to Window option is turned on. Otherwise, the default size policy (preferred) will make scroll bars appear when the scroll area becomes smaller than the label's minimum size hint.
Setting it to ignored fits to the window, as expected and as stated. Setting it to preferred provides scroll bars when the image is larger than the scroll area, also as expected and as stated. My issue is that when the sizepolicy is set to preferred, the resize function of the QLabel doesn't work. It always stays at the default size of the loaded image. The only way that I'm able to get the resize function to work is when I don't assign a layout/break the layout to the widget in the QScrollArea, but then no scrollbars will appear when the image is larger than the QScrollArea.
Does anyone have any ideas of how to make the resize function and scrollbars work at the same time?
Thanks in advance for any help. I'm trying to learn qt5 still and this seems like it'd be a simple thing to do, but it's slowly driving me crazy.
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'm trying to make my UI content expand nicely when the user resizes the window. I'm doing this in Qt Designer and learning about layouts and size policies.
Seems that everything is working fine for now: one layout stays within its maximum size, while another expands with the window resize, and they all stay above their minimum sizes. That's great and all, but the problem is that if I have a list with a lot of items, or if I am displaying a very large image, it will expand beyond the available window space and cause the window to be huge.
How can I specify something along the lines of "do not expand beyond the available window space"? I've played around with the size policies, but I couldn't get it to work. Is this something I need to set for the form itself rather than the layouts it contains?
I should specify this is the desired behavior: Display the widget as large as the available window, even if the widget content is too small. Expand/Shrink the widget to fill the window when the window is resized. Do not expand beyond the available window space. The widgets in question are 2 images (labels) and 1 list view.
I set the size policy to "ignored" for the respective widgets. That fixed it.
Or you can set the window maximum size.
I am working on a project where I have to display a pretty large (vertically) main Widget.
In the initial Version of my GUI it was just added as the central Widget of a QMainWindow, which caused the Problem that on small screen resolutions the controls on the Bottom of the Widget are unreachable.
To solve this i wrapped a QScrollArea around the main Widget, but now the main window is always relatively small even if it doesn't have to.
What do i need to change so the Main Windows (vertical) size is large enough to show all the contents unless it would be too large for the screen resolution? Also I don't want it to be stretched, so simply always using the whole vertical screen resolution is not an option. Ideally the size should be fixed to the size needed by the contents (w/o the scroll area) and only smaller where needed.
Overriding the sizeHint method did only resulted in a small enlargement of the Window and setting the minimal height brings me back to the beginning where some of the controls are not assessable on small resolutions.
Since i am new to QT I am actually out of ideas how to google the solution because most Solutions I can find are about sizing components inside a Window and not the Window itself.
By default a QScrollArea will not attempt to expand to fit its contents. In order to do this you will need to re-implement QScrollArea's sizeHint() to return the size of QScrollArea's child widgets.
In your question it sounds like you were trying to re-implement MainWindow's sizeHint? re-implementing sizeHint on the top-level window will have no effect as sizeHint designed for use with widgets inside layouts.
I am new to QT. I'm trying to understand the layout mechanism by trying to implement this small window seen below. It has the following elements under the QWidget that's the main window:
One big QWidget that stretches on all the client area.
Two QWidget containers on the top of the window. Both should have the same height, but the right one stretches horizontally, as the window grows/shrinks.
one button container widget on the top right, with fixed height and width
Large QWidget container filling the rest of the client area, that should resize as the window resizes.
The parent window itself is resizeable.
I'm looking for hints as to what layout I should use. How do I achieve this programatically? define what stretches automatically, what stays with a fix size? and how the proportions are kept where they need to be kept.
I'd appreciate any pointer you may have.
The easiest, and IMHO best, way to accomplish this is via the QHBoxLayout and QVBoxLayouts. You can do this via the designer in QtCreator, but I find it doesn't work perfectly if you need to adapt things over time. If it's a static set of widgets, I do suggest designing it using the QtCreator designer as it'll greatly simplify your life.
If you're going to do it programatically, the main window should be set to use a QVBoxLayout and then two sub-QVBoxLayout's after that, where the bottom one is configured to take any space it can get. Then in the top QVBoxLayout, add a QHBoxLayout with your two upper components.
to set a widget to fixed size in code you call setFixedSize( int h, int w ) on the widget. To do it in Designer click on the widget and look in the property editor in the QWidget section. open the sizePolicy thingy and set horizontal and/or vertical to fixed. Then open Geometry and set the width and Height.
To make them stretch at different ratios in code you use a separate argument when using a box layout. eg layout->addWidget( button1, 1 ); layout->addWidget (button2, 2); this would cause button2 to expand at twice the rate of button1. To do this in designer, open the sizePolicy property of the widgets and set the HorizontalStrech and/or VerticalSretch. Note that the size policy needs to not be Fixed in this case for the direction you want to set the stretch on. Also it will never let a widget shrink below its minimum size (it would rather mess up the ratio than shrink something too small).