2011-04-26 106 views
0

我正在研究基本的图像查看器/标记器,它需要缩略图来选择图像。到目前为止,我已经使用QDockWidget封装了QScrollArea和QHBoxLayout以包含一系列QLabel,每个QLabel都设置了其QPixMap。如何创建可调整大小的Qt缩略图预览?

这看起来很不雅观,而且只有当我调整QDockWidget的大小时才考虑如何实现缩略图的自动缩放。当滚动条出现并消失时,需要调整缩略图的额外需要使其更加复杂。

必须有更好的方法来做到这一点?

回答

1

当我尝试使用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); 
    } 

}

相关问题