问题陈述说明了一切。我能够放大QMdiSubWindow(并且像素图向上缩放,即适当地放大),但我无法减小它的大小(即缩小或缩小图像)。但是,当尝试缩小时,QMdiSubWindow 的边框句柄仍然是固定不变的。
我正在使用的代码如下:
class PixmapWidget : public QLabel
{
Q_OBJECT
public:
PixmapWidget():
QLabel()
{
_pixmap = QPixmap("path\\to\\image.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();
}