Qt覆盖QLabel PaintEvent

时间:2016-10-21 03:22:13

标签: c++ image qt

在过去的几天里,我一直在努力解决这个问题。当用户调整窗口大小时,我希望能够在QLabel中增加和缩小分配的Pixmap。问题是保持纵横比和图像质量。这里的另一个用户建议我重新实现标签的油漆事件 - 但我仍然非常迷失。我甚至不确定我是否正确覆盖了paintEvent。我会在这里找一些示例代码。

这就是我所在的地方:

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
}

2 个答案:

答案 0 :(得分:1)

这是一个QLabel子类的可能实现,它可以在保持其宽高比的同时缩放其pixmap内容。它是基于QLabel::paintEvent is implemented

的方式实施的

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

#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();
}

我认为这比this answer中的解决方案更好,因为QLabel在这里负责调整其像素图的大小。因此,无需在每次调整父窗口小部件时手动调整其大小,并且每次在其上设置新的像素图时都不需要。

答案 1 :(得分:0)

首先,我要感谢迈克。

此外,我想在2016年10月21日之前添加他的答案-但是到目前为止我还无法添加评论-因此这是一个完整的答案。 [如果任何人都可以将其移至评论部分,请放心。]

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

标题:

df.PROD_NBR.astype(float)

模块:

products_df.merge(postransaction_df, products_df, on='PROD_NBR')