首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Qt重写QLabel PaintEvent

Qt重写QLabel PaintEvent
EN

Stack Overflow用户
提问于 2016-10-21 03:22:13
回答 2查看 8.8K关注 0票数 1

在过去的几天里,我一直在努力解决这个问题。当用户调整窗口大小时,我希望能够扩展和缩小QLabel中指定的Pixmap。问题是如何保持高宽比和图像质量。这里的另一个用户建议我为标签重新实现画图事件--但我仍然非常迷茫。我甚至不确定我是否正确地重写了paintEvent。我会在这里为一些示例代码而杀人。

我现在在这里:

代码语言:javascript
复制
void MyLabel::paintEvent(QPaintEvent * event)
{
    //if this widget is assigned a pixmap
    //paint that pixmap at the size of the parent, aspect ratio preserved
    //otherwise, nothing
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-21 09:04:24

下面是一个QLabel子类的可能实现,该子类可以缩放其像素映射内容,同时保持其纵横比。它是基于QLabel::paintEvent 已实现的方式实现的。

如果在布局中使用它,还可以将其大小策略设置为QSizePolicy::Expanding,以便QLabel占用布局中的额外空间来更大地显示像素地图内容。

代码语言:javascript
复制
#include <QApplication>
#include <QtWidgets>

class PixmapLabel : public QLabel{
public:
    explicit PixmapLabel(QWidget* parent=nullptr):QLabel(parent){
        //By default, this class scales the pixmap according to the label's size
        setScaledContents(true);
    }
    ~PixmapLabel(){}

protected:
    void paintEvent(QPaintEvent* event);
private:
    //QImage to cache the pixmap()
    //to avoid constructing a new QImage on every scale operation
    QImage cachedImage;
    //used to cache the last scaled pixmap
    //to avoid calling scale again when the size is still at the same
    QPixmap scaledPixmap;
    //key for the currently cached QImage and QPixmap
    //used to make sure the label was not set to another QPixmap
    qint64 cacheKey{0};
};

//based on the implementation of QLabel::paintEvent
void PixmapLabel::paintEvent(QPaintEvent *event){
    //if this is assigned to a pixmap
    if(pixmap() && !pixmap()->isNull()){
        QStyle* style= PixmapLabel::style();
        QPainter painter(this);
        drawFrame(&painter);
        QRect cr = contentsRect();
        cr.adjust(margin(), margin(), -margin(), -margin());
        int align= QStyle::visualAlignment(layoutDirection(), alignment());
        QPixmap pix;
        if(hasScaledContents()){ //if scaling is enabled
            QSize scaledSize= cr.size() * devicePixelRatioF();
            //if scaledPixmap is invalid
            if(scaledPixmap.isNull() || scaledPixmap.size()!=scaledSize
                    || pixmap()->cacheKey()!=cacheKey){
                //if cachedImage is also invalid
                if(pixmap()->cacheKey() != cacheKey){
                    //reconstruct cachedImage
                    cachedImage= pixmap()->toImage();
                }
                QImage scaledImage= cachedImage.scaled(
                            scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
                scaledPixmap= QPixmap::fromImage(scaledImage);
                scaledPixmap.setDevicePixelRatio(devicePixelRatioF());
            }
            pix= scaledPixmap;
        } else { // no scaling, Just use pixmap()
            pix= *pixmap();
        }
        QStyleOption opt;
        opt.initFrom(this);
        if(!isEnabled())
            pix= style->generatedIconPixmap(QIcon::Disabled, pix, &opt);
        style->drawItemPixmap(&painter, cr, align, pix);
    } else { //otherwise (if the label is not assigned to a pixmap)
        //call base paintEvent
        QLabel::paintEvent(event);
    }
}

//DEMO program
QPixmap generatePixmap(QSize size) {
    QPixmap pixmap(size);
    pixmap.fill(Qt::white);
    QPainter p(&pixmap);
    p.setRenderHint(QPainter::Antialiasing);
    p.setPen(QPen(Qt::black, 10));
    p.drawEllipse(pixmap.rect());
    p.setPen(QPen(Qt::red, 2));
    p.drawLine(pixmap.rect().topLeft(), pixmap.rect().bottomRight());
    p.drawLine(pixmap.rect().topRight(), pixmap.rect().bottomLeft());
    return pixmap;
}


int main(int argc, char *argv[])

{
    QApplication a(argc, argv);


    QPixmap pixmap= generatePixmap(QSize(1280, 960));
    PixmapLabel label;
    label.setPixmap(pixmap);
    label.setAlignment(Qt::AlignCenter);
    label.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    label.setMinimumSize(320, 240);
    label.show();

    return a.exec();
}

我认为这比这个答案中的解决方案要好,因为这里的QLabel负责调整其像素映射的大小。因此,每次调整父部件的大小和每次设置新的像素映射时,都不需要手动调整它的大小。

票数 1
EN

Stack Overflow用户

发布于 2019-08-18 18:40:43

首先,我想感谢迈克。

此外,我还想补充一下他从2016年10月21日开始的答复--不过,到目前为止,我还不能补充评论--因此,这里有一个完整的答案。如果有人能够将其移到评论区,请随意使用。

我还添加了QLabel的其他构造函数的覆盖和带有默认参数的窗口标志(如QLabel ),并添加了与Qt框架兼容的Q_OBJECT宏:

标题:

代码语言:javascript
复制
class PixmapLabel : public QLabel
{
    Q_OBJECT
public:
    explicit PixmapLabel(QWidget* parent=nullptr, Qt::WindowFlags f = Qt::WindowFlags());
    explicit PixmapLabel(const QString &text, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
   ...
}

模块:

代码语言:javascript
复制
PixmapLabel::PixmapLabel(QString const & text, QWidget * parent, Qt::WindowFlags f) :
    QLabel(text, parent, f)
{
    //By default, this class scales the pixmap according to the label's size
     setScaledContents(true);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40167841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档