Introduction
I am working with the Designer inside the Qt Creator and have a QMainWindow
with a QLabel
in it. Because the program loads pictures and displays them inside the label i want that the label resizes with a ratio of 1.25 inside setted boundaries when expanding or shrinking the QMainWindow
. The label should resize INDEPENDENT, again INDEPENDENT from its content.
What i want:
- Open the main window for first time:
width: 640, height: 512 - Shrinking the main window:
label shrinks with constant ratio (640/512) till minimum size (320 x 256) - Expanding the main window:
label expands with constant ratio (640/512) till maximum size (1280 x 1024)
1. Approach:
Therefor i...
- added a
QLabel
(calledimageLabel
) inside thecentralWidget
of theQMainWindow
- set the
centralWidget
's layout to grid layout (QGridLayout
) - sed the following properties to the
QLabel
:- geometry - Can not set the values because of grid layout usages!
- minimumSize > width: 320, height: 256 (Minimum values)
- maximumSize > width: 1280, height: 1024 (Maximum values)
- sizePolicy > Horizontal Policy == Vertical Policy == Expanding
Element structure:
This doesn't work because i can not set an initial size in the 'geometry' section. The label does not scale with fixed ratio although it respects minimum and maximum values.
2. Approach:
Following that answer i set an initial pixmap:
QPixmap p;
ui->imageLabel->setPixmap(p.scaled(640, 512, Qt::KeepAspectRatio));
Which didn't change anything.
3. Approach:
I also applied the other answer's class and promoted it to the widget:
That didn't change anything too.
4. Approach:
I then combined the 2. and 3. approach and set an initial pixmap which...
...didn't change anything.
Here is what it does for the approaches 1. - 4.:
5. Approach
Adding the label of 4. approach into a widget:
Well the label doesn't resize at all:
So, how can get the label to have an initial size of 640 x 512 and scale with fixed ratio between 1280 x 1024 and 320 x 256 ?