【发布时间】:2021-12-20 23:59:48
【问题描述】:
我正在使用 QPixmap 在 QLabel 中显示图像,当图像放大到超出其当前所在的父窗口小部件布局的范围时,窗口会重新调整大小以适应它,然后我也不能缩小窗口并缩小图像。是否需要将特定的 QSizePolicy 放在父主窗口布局、标签本身或其他一些设置功能上?
或者是一种解决方案,不是放大图像内容以使其最终超出标签范围,而是需要在图像中的某个点进行裁剪,使其永远不会超出标签?
w_content = new Content;
QStackedLayout *s_layout = new QStackedLayout(contentContainer);
s_layout->setStackingMode(QStackedLayout::StackAll);
s_layout->setSpacing(0);
s_layout->setContentsMargins(0, 0, 0, 0);
s_layout->addWidget(w_content);
Content::Content(QWidget *parent) : QWidget(parent), contentLabel(new QLabel) {
QVBoxLayout *c_layout = new QVBoxLayout(this);
contentLabel->setBackgroundRole(QPalette::Base);
contentLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
contentLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
contentLabel->setScaledContents(false);
contentImage = new QImage("C:\\Users\\user\\Downloads\\test.png");
contentPixmap = QPixmap::fromImage(*contentImage);
contentLabel->setPixmap(contentPixmap);
c_layout->setSpacing(0);
c_layout->setContentsMargins(0, 0, 0, 0);
c_layout->addWidget(contentLabel);
this->setLayout(c_layout);
}
void Content::scaleImage(double factor) {
m_scaleFactor *= factor;
w = contentPixmap.width()*m_scaleFactor;
h = contentPixmap.height()*m_scaleFactor;
QPixmap pm = contentPixmap.scaled(w, h, Qt::KeepAspectRatio, Qt::FastTransformation);
contentLabel->setPixmap(pm);
}
【问题讨论】: