Automagically resize window
-
I have a simple window, that has a QLabel in it. I want to draw various images into the pixmap. How do I get it to resize the window when I create a new QPixmap for the QLabel? Is there some kind of Layout I need to use, or some way I tell the QDialog to recheck its layout? If I set a vertical layout on the dialog, and set it to a fixed constraint, it resizes correctly, but then the user can't resize the window.
-
When a widget is added to a layout it automatically resizes when it's contents changes, doesn't it?
For example,
@#include <QApplication>
#include <QtGui>int main(int argc, char* argv[])
{
QApplication app(argc, argv);QWidget w;
QVBoxLayout mainLayout(&w);QLabel l;
mainLayout.addWidget(&l);l.setPixmap(QPixmap("1.png"));
l.setPixmap(QPixmap("2.png"));
w.show();return app.exec();
}@1.png and 2.png are of different sizes. But when the widget is shown the entire image is visible.
Thus you must add your QLabel to a QLayout. It will automatically resize when you call QLabel::setPixmap(). -
vc15, one problem I think with the above would be (without actually trying the code), the label will resize to a large pixmap, and further scale up if another larger pixmap is loaded. but then if you load a smaller pixmap, it won't scale back. this is something i have observed ...
-
Okay, here's some more info, and I can see where chetankjain sees the problem. If you do a resize on the widget, things get a little trickier. If you resize it to be smaller than the pixmap, it will change size to be bigger. But if the resize is bigger, it won't change size to be smaller. So the following creates a 400x400 widget, with the label inside of it:
@#include <QApplication>
#include <QtGui>int main(int argc, char* argv[])
{
QApplication app(argc, argv);QWidget w;
w.resize(400,400);QVBoxLayout mainLayout(&w);
QLabel l;
mainLayout.addWidget(&l);l.setPixmap(*new QPixmap(100,200));
w.show();return app.exec();
}
@This iseven more important because Widgets (& dialogs) created by Qt Creator always have a resize call when they get created behind the curtain. So the trick is to resize the widget to be smaller than the smallest pixmap you can imagine getting inserted.