微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

以Qt显示图像以适合标签尺寸

如何解决以Qt显示图像以适合标签尺寸

| 我已经尝试了几种在窗体上显示图像的方法,但是没有一种方法可以按照我的意愿工作。 我读过很多地方,最简单的方法是创建标签并使用它来显示图像。我有一个标签,其大小由布局指定,但是如果我使用像素图将图像加载到其中,则该标签的大小将调整为图像的大小。如果我将img标签用作文本或CSS背景属性,则不会显示整个图像。我想做的是加载图像并使其适合标签,而不更改标签的大小,但是当我调整窗口大小并同时调整标签大小时,图像也应重新调整大小。将永远适合它。 如果唯一的方法获取标签的大小,并调整像素图的大小以使其适合并处理调整大小事件(信号),那么如何调整像素图的大小?我希望我不需要每次都将整个内容保存到QImage中并创建一个像素图。 另外,我如何居中?如果无法同时满足宽度和高度要求,我希望较小的尺寸居中。 哦,我也不想使用滑块来处理溢出。     

解决方法

QLabel :: setScaledContents(bool)有帮助吗?在图像查看器示例中也可能会有一些有用的信息。     ,对于这个问题,实际上有一个非常简单的解决方案。您应该修改两件事: 将缩放的内容设置为true(如上所述) 将标签的尺寸政策设置为忽略
QLabel lblImage;

lblImage->setPixmap( QPixmap( \"big_image.jpg\" ) );

lblImage->setScaledContents( true );

lblImage->setSizePolicy( QSizePolicy::Ignored,QSizePolicy::Ignored );
如果the1自动调整尺寸,图像将拉伸到标签的尺寸。     ,保留您的原始
pixmap
的副本。然后将“ 3”信号连接到实现此目的的插槽(或重写“ 4”功能):
lblImage->setPixmap(pixmap.scaled(lblImage->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
    ,我还将回答我自己的问题,但不会将其标记为解决方案,因为我要求的是上面给出的一个简单问题。毕竟,我最终使用了不太简单的解决方案,因此任何需要做类似事情并且有时间使用它的人,这里都是我的最终工作代码。这个想法是扩展QLabel并重载setPixmap和drawEvent方法。 QPictureLabel.hpp(头文件)
#include \"QImage.h\"
#include \"QPixmap.h\"
#include \"QLabel.h\"

class QPictureLabel : public QLabel
{
private:
    QPixmap _qpSource; //preserve the original,so multiple resize events won\'t break the quality
    QPixmap _qpCurrent;

    void _displayImage();

public:
    QPictureLabel(QWidget *aParent) : QLabel(aParent) { }
    void setPixmap(QPixmap aPicture);
    void paintEvent(QPaintEvent *aEvent);
};
QPictureLabel.cpp(实现)
#include \"QPainter.h\"

#include \"QPictureLabel.hpp\"

void QPictureLabel::paintEvent(QPaintEvent *aEvent)
{
    QLabel::paintEvent(aEvent);
    _displayImage();
}

void QPictureLabel::setPixmap(QPixmap aPicture)
{
    _qpSource = _qpCurrent = aPicture;
    repaint();
}

void QPictureLabel::_displayImage()
{
    if (_qpSource.isNull()) //no image was set,don\'t draw anything
        return;

    float cw = width(),ch = height();
    float pw = _qpCurrent.width(),ph = _qpCurrent.height();

    if (pw > cw && ph > ch && pw/cw > ph/ch || //both width and high are bigger,ratio at high is bigger or
        pw > cw && ph <= ch || //only the width is bigger or
        pw < cw && ph < ch && cw/pw < ch/ph //both width and height is smaller,ratio at width is smaller
        )
        _qpCurrent = _qpSource.scaledToWidth(cw,Qt::TransformationMode::FastTransformation);
    else if (pw > cw && ph > ch && pw/cw <= ph/ch || //both width and high are bigger,ratio at width is bigger or
        ph > ch && pw <= cw || //only the height is bigger or
        pw < cw && ph < ch && cw/pw > ch/ph //both width and height is smaller,ratio at height is smaller
        )
        _qpCurrent = _qpSource.scaledToHeight(ch,Qt::TransformationMode::FastTransformation);

    int x = (cw - _qpCurrent.width())/2,y = (ch - _qpCurrent.height())/2;

    QPainter paint(this);
    paint.drawPixmap(x,y,_qpCurrent);
}
用法:与在setScaledContents之外使用普通标签显示图像相同
img_Result = new QPictureLabel(ui.parent);
layout = new QVBoxLayout(ui.parent);
layout->setContentsMargins(11,11,11);
ui.parent->setLayout(layout);
layout->addWidget(img_Result);

//{...}

QPixmap qpImage(qsImagePath);
img_Result->setPixmap(qpImage);
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。