当我尝试使用qpixmap动画调整qlabel的大小时,我遇到了类似的问题。我发现最好的方法是使用QWidget,然后重新实现paintEvent函数。然后你的QWidget图像会自动缩放,如果它调整大小。这里有一个例子:
在我来说,我曾在一个私有对象的私有变量叫private_:
bool image_set_;
QImage image_;
QBrush paintbrush_;
void MyClass::paintEvent(QPaintEvent* event)
{
// if the QWidget has an image set, then we use our custom painting.
if(this->private_->image_set_)
{
//I've made it so that my QWidget has a 1px white border
this->private_->paintbrush_.setTextureImage(this->private_->image_.scaled(QSize(this->width() - 2, this->height() - 2)));
QPainter painter(this);
QRect temp_rect = QRect(1, 1, this->width()-2, this->height() - 2);
painter.fillRect(this->rect(), Qt::white);
painter.fillRect(temp_rect, this->private_->paintbrush_);
}
else
{
QWidget::paintEvent(event);
}
}