簡體   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