我正在开发一个基本的图像查看器/标记器,需要缩略图视图才能选择图像。到目前为止,我已经使用了一个QDockWidget,其中包含一个带有QHBoxLayout的QScrollArea,以包含一系列QLabel,每个QLabel都设置了QPixMap。
这看起来非常不优雅,只考虑在调整QDockWidget大小时如何实现缩略图的自动缩放变得更加丑陋。当滚动条出现并消失时,需要另外调整缩略图的大小,这一点变得更加复杂。
必须有更好的方法吗?
答案 0 :(得分:2)
在尝试使用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 );
}
}