最簡單的方法是爲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的不會管理調整的窗口小部件的塗裝。)
編輯:
現在我只想覆蓋以嶄新的形象(尺寸30×30)的背景 使用「Qpainter.drawImage」是移動在背景 圖像(1366x768)。它類似的信息(在 Qlabel PNG圖片)的 畫面上移動的MousePointer其中屏幕是所述背景形式&所述的MousePointer作爲newimage使用動態繪製 「Qpainter.drawImage」
爲了實現這個你可以做一個簡單的編輯/過載到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);
}
但我有一個背景UI(1366×768)和它的變化是根據它的功能(獨立的),它可以」不能改變。現在,我只想使用在背景圖像(1366x768)上移動的「Qpainter.drawImage」將新背景圖像(尺寸30x30)疊加在背景上。這就像在屏幕上作爲背景窗體(Qlabel上的.png圖像)上移動鼠標指針並且鼠標指針是使用'Qpainter.drawImage'動態繪製的新圖像' – user2164302 2013-03-13 08:30:47
看到我上面的編輯,看看是否解決了你的問題題。 – 2013-03-13 19:42:42