【问题标题】:Qt Don't resize window when scaled QPixmap exceeds boundsQt 当缩放的 QPixmap 超出范围时不要调整窗口大小
【发布时间】: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);
}

【问题讨论】:

    标签: c++ windows qt qpixmap


    【解决方案1】:

    原来是一个非常简单的解决方案,这让我觉得有点愚蠢。将QSizePolicy::Ignored 用于contentLabel->setSizePolicy 中的hor/vert 使窗口不会调整大小以适应图像标签,因此图像可以放大到小部件边界之外。只要图像放大后能够缩小尺寸,从调整大小事件中获取父小部件大小并将内容标签大小设置为它允许再次缩小图像。

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 1970-01-01
      • 2017-08-15
      • 2015-12-24
      • 2012-12-21
      • 1970-01-01
      • 2013-12-22
      • 2015-12-27
      • 1970-01-01
      相关资源
      最近更新 更多