PDA

View Full Version : Resizing QLabel inside ResizeEvent



alexrn
24th March 2016, 19:34
Hello, I've been reading about what I'm gonna ask, but I didn't find a solution.

I have a QImage on a QLabel, and I want to resize that image (and the QLabel, of course) when the window of my App is resized, all I have read is that I have to use layouts, but I don't understand how, cause the adjustSize() layout only changes the position of my label and what I want is to change it label->width() and label->height().

I have a little code that override the virtual method resizeEvent:


void MainWindow::resizeEvent(QResizeEvent *event){

QSize oldSize = event->oldSize();
QSize actualSize = event->size();

if(ui->label->isVisible()){

double width = actualSize.width() / (double)oldSize.width(),
height = actualSize.height() / (double)oldSize.height();

ui->label->resize(ui->label->size().width() * width,
ui->label->size().height() * height);
showImage(actualImage);
}
}


but it doesn't work properly, it resizes the photo, but when I resize the window many times, the label is getting smaller each time until it disappears. I'm pretty sure that the problem here is about the int var, because it loses information each time I resize.

Which is the best way to do what I want?

Thanks!

ChrisW67
24th March 2016, 21:00
Put the QLabel in a layout applied to the MainWindow container widget.
Set the QLabel's scaledContent property.

alexrn
25th March 2016, 14:16
Put the QLabel in a layout applied to the MainWindow container widget.
Set the QLabel's scaledContent property.

Hi, thanks for answering,

I tried your solution, but it didn't work for me, the scaledContent property only resizes the photo when I set the Pixmap into the QLabel, but I do that manually, so I think it is not needed here. As I said, what I want is the QLabel resizing with the App window. I don't know if I'm explaining well what I mean.

When I try, for example, a vertical layout, my QLabel expands automatically, and I don't want that, I want it in a specific side.

Regards.

ChrisW67
25th March 2016, 20:30
As I said, what I want is the QLabel resizing with the App window


When I try, for example, a vertical layout, my QLabel expands automatically, and I don't want that, I want it in a specific side.
So you want the QLabel to simultaneously resize with the containing window and not resize with the containing window? Perhaps you can see the potential for confusion.

Put the QLabel in a QGridLayout with some spacers or other widgets in the other cells and set the stretch factors.

alexrn
26th March 2016, 00:41
So you want the QLabel to simultaneously resize with the containing window and not resize with the containing window? Perhaps you can see the potential for confusion.

Put the QLabel in a QGridLayout with some spacers or other widgets in the other cells and set the stretch factors.

I wanted it to resize, but not to expand at the whole window, using the stretch factors did the trick, thanks so much!