繁体   English   中英

如何在Qt中使用jpeg / png图像的背景窗体上使用QPainter.drawImage绘制jpeg / png图像?

[英]How to draw a jpeg/png image using QPainter.drawImage on a background form with jpeg/png image in Qt?

我在Qt中使用QLabel创建了一个表单 ,其中包含.png图像作为背景图像。 现在,我想在这样的背景下PNG图片绘制PNG图片。 我可以在背景上绘制图像,如果没有.png / .jpeg图像作为背景,即,可以以纯格式显示而没有任何背景图像。 每当我尝试在背景图像上使用QPainter.drawimage绘制图像时,背景图像仅可见,即背景静态图像(.png)会动态叠加图像('.png'使用QPainter.drawImage)画。

谁能让我知道这种方法的解决方案。 如果没有,请告诉我其他方法。 提前致谢 :)

最简单的方法是将QLabel子类化,添加其他方法来接受前景/背景像素图,并覆盖paintEvent函数。

其他方法可能如下所示:

// literally the same thing as setPixmap but constructed a new function to be clearer
void CustomLabel::setBackground(const QPixmap & pixmap)
{ 
    // will handle sizing the label to the size of the image
    // and will additionally handle drawing of the background
    this->setPixmap(pixmap);
} 

void CustomLabel::setForeground(const QPixmap & pixmap)
{ 
    // create member variable that points to foreground image
    this->foreground = pixmap;
} 

然后,重写paintEvent函数:

void CustomLabel::paintEvent(QPaintEvent * e)
{
    // use the base class QLabel paintEvent to draw the background image.
    QLabel::paintEvent(e); 

    // instantiate a local painter
    QPainter painter(this);

    // draw foreground image over the background
    // draws the foreground starting from the top left at point 0,0 of the label.
    // You can supply a different offset or source/destination rects to achieve the 
    // blitting effect you want.
    painter.drawPixmap(QPoint(0,0),this->foreground); 

}

...然后您可以按以下方式使用标签:

//instantiate a custom label (or whatever you choose to call it)
label = new CustomLabel();

// use the additional methods created as part of your CustomLabel class
label->setBackground(QPixmap("background.png"));
label->setForeground(QPixmap("foreground.png"));

此外,CustomLabel类可以进一步扩展以接受更多的内容,而不仅仅是单个背景和前景图像。 例如, setPixmaps(QVector<QPixmap>)函数可以存储所传递图像的矢量,将标签的大小调整为矢量中的第一张图像,然后利用paintEvent函数绘制所有传递给它的图像。

请记住,前景图像的尺寸应小于或等于背景图像,以免前景图像被裁剪。 (因为QPainter不会调整其小部件的绘画大小。)

编辑:

现在,我只想使用在背景图像(1366x768)上移动的'Qpainter.drawImage'在背景上覆盖一个新图像(大小为30x30)。 这就像在屏幕上移动鼠标指针一样,其中屏幕是背景形式(Qlabel上的.png图像),而鼠标指针是使用'Qpainter.drawImage'动态绘制的newimage。

为此,您可以对setForeground函数进行简单的编辑/重载,并像下面这样修改paintEvent函数:

void CustomLabel::setForeground(const QPixmap & pixmap, const QPointF & offset)
{ 
    // create member variable that points to foreground image
    this->foreground = pixmap;

    // establish the offset from the top left corner of the background image
    // to draw the top left corner of the foreground image.
    this->foregroundOffset = offset;
} 

void CustomLabel::paintEvent(QPaintEvent * e)
{
    // use the base class QLabel paintEvent to draw the background image.
    QLabel::paintEvent(e); 

    // instantiate a local painter
    QPainter painter(this);

    // draw foreground image over the background using given offset
    painter.drawPixmap(this->foregroundOffset,this->foreground); 

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM