Reducing the size of a QMdiSubWindow holding a QLabel with QPixmap
-
The question statement says it all. I am able to enlarge the QMdiSubWindow (and the pixmap scales UP, i.e. enlarges, appropriately) but I am not able to reduce its size (i.e. scale the image DOWN or shrink). When trying to scale down though, the border handles of the QMdiSubWindow remain rigidly fixed.
The code I am using is as follows:
@class PixmapWidget : public QLabel
{
Q_OBJECT
public:
PixmapWidget():
QLabel()
{
_pixmap = QPixmap("path\to\images.jpg");
setPixmap(_pixmap);
_layout = new QHBoxLayout();
_layout->setSizeConstraint(QLayout::SetNoConstraint);
setLayout(_layout);
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
}void resizeEvent(QResizeEvent * event) { int width = event->size().width(); int height = event->size().height(); setPixmap(_pixmap.scaled(width,height)); QLabel::resizeEvent(event); }
protected:
QPixmap _pixmap;
QHBoxLayout* _layout;
};int main()
{
QMainWindow mainWindow;
QHBoxLayout* layout = new QHBoxLayout();
QMdiArea* mdiArea = new QMdiArea();mainWindow.setCentralWidget(mdiArea); mainWindow.centralWidget()->setLayout(layout); layout->setSizeConstraint(QLayout::SetNoConstraint); QMdiSubWindow* mdiSubWindow = new QMdiSubWindow(); layout->addWidget(mdiSubWindow); mdiSubWindow->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred)); mdiSubWindow->layout()->setSizeConstraint(QLayout::SetNoConstraint); DMSQt::PixmapWidget pixmapWidget; mdiSubWindow->layout()->addWidget(&pixmapWidget); mainWindow.show(); qapp.exec();
}@
-
Alright, I found the problem. Apparently, I need to set a minimum size on my text label:
Adding:
@setMinimumSize(_pixmap.width(), _pixmap.height());@
solved the problem.