【问题标题】:How to create a resizable Qt thumbnail preview?如何创建可调整大小的 Qt 缩略图预览?
【发布时间】:2011-04-26 03:38:16
【问题描述】:

我正在开发一个基本的图像查看器/标记器,它需要缩略图视图来选择图像。到目前为止,我使用了一个 QDockWidget,它包含一个 QScrollArea 和一个 QHBoxLayout 来包含一系列 QLabel,每个 QLabel 都有其 QPixMap 集。

这看起来很不雅,而且当 QDockWidget 调整大小时,考虑如何实现缩略图的自动缩放只会变得更丑陋。当滚动条出现和消失时,还需要调整缩略图的大小,这使情况更加复杂。

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

【问题讨论】:

    标签: c++ qt qt4 thumbnails preview


    【解决方案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 );
        }
    

    }

    【讨论】:

      猜你喜欢
      • 2013-06-22
      • 2013-06-24
      • 2012-07-15
      • 2011-11-04
      • 2020-02-29
      • 2013-07-21
      • 2011-11-19
      • 2014-03-10
      • 2014-09-21
      相关资源
      最近更新 更多