一個 Qt 顯示圖片的控件(繼承QWidget,使用QPixmap記錄圖像,最后在paintEvent進行繪制,可縮放)


Qt 中沒有專門顯示圖片的控件,通常我們會使用QLabel來顯示圖片。但是QLabel 顯示圖片的能力還是有點弱。比如不支持圖像的縮放一類的功能,使用起來不是很方便。因此我就自己寫了個簡單的類。

 

我這個類支持三種圖像顯示模式,我分別稱之為:FIXED_SIZE, CENTRED,AUTO_ZOOM, AUTO_SIZE。

 

  • FIXED_SIZE 模式下,顯示的圖像大小等於圖像尺寸乘以縮放因子,如果控件的尺寸小於這個大小則多出的部分被裁切掉。
  • FIX_SIZE_CENTRED模式與FIXED_SIZE 模式類似,但是,顯示的圖像居於窗口正中。
  • AUTO_ZOOM 模式下,顯示的圖像大小自動適應控件窗口大小。
  • AUTO_SIZE 模式下,這個控件的尺寸等於圖像尺寸乘以縮放因子。

 

 

下面是代碼:

[cpp]  view plain  copy
 
  1. #ifndef PICTUREBOX_H  
  2. #define PICTUREBOX_H  
  3.   
  4. #include <QWidget>  
  5. #include <QImage>  
  6. #include <QPixmap>  
  7.   
  8.   
  9. class PictureBox : public QWidget  
  10. {      
  11.     Q_OBJECT  
  12. public:  
  13.     enum PB_MODE {FIXED_SIZE, FIX_SIZE_CENTRED, AUTO_ZOOM, AUTO_SIZE};  
  14.     explicit PictureBox(QWidget *parent = 0);  
  15.     void setMode(PB_MODE mode);  
  16.     ~PictureBox();  
  17. private:  
  18.     QPixmap m_pixmap;  
  19.     double m_scale;  
  20.     PB_MODE m_mode;  
  21.     QBrush m_brush;  
  22. protected:  
  23.     void paintEvent(QPaintEvent * event);  
  24. signals:  
  25.   
  26. public slots:  
  27.     bool setImage(QImage &image, double scale = 1.0);  
  28.     void setBackground(QBrush brush);  
  29. };  
  30.   
  31. #endif // PICTUREBOX_H  

 

[cpp]  view plain  copy
 
  1. #include "picturebox.h"  
  2. #include <QPainter>  
  3. #include <QDebug>  
  4. static const int IMAGE_WIDTH = 160;  
  5. static const int IMAGE_HEIGHT = 120;  
  6. static const QSize IMAGE_SIZE = QSize(IMAGE_WIDTH, IMAGE_HEIGHT);  
  7.   
  8. PictureBox::PictureBox(QWidget *parent) : QWidget(parent)  
  9. {  
  10.     m_pixmap = QPixmap(IMAGE_SIZE);  
  11.     m_pixmap.fill();  
  12.     m_scale = 1.0;  
  13.     m_mode = FIXED_SIZE;  
  14.     m_brush = QBrush(Qt::white);  
  15. }  
  16.   
  17. void PictureBox::setBackground(QBrush brush)  
  18. {  
  19.     m_brush = brush;  
  20.     update();  
  21. }  
  22.   
  23. void PictureBox::setMode(PB_MODE mode)  
  24. {  
  25.     m_mode = mode;  
  26.     if(m_mode == AUTO_SIZE)  
  27.     {  
  28.         setFixedSize(m_pixmap.size() * m_scale);  
  29.     }  
  30.     else  
  31.     {  
  32.         setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);  
  33.         setMinimumSize(0, 0);  
  34.     }  
  35.     update();  
  36. }  
  37.   
  38. bool PictureBox::setImage(QImage &image, double scale)  
  39. {  
  40.     if(image.isNull())  
  41.     {  
  42.         return false;  
  43.     }  
  44.     m_pixmap = QPixmap::fromImage(image);  
  45.     m_scale = qBound(0.01, scale, 100.0);  
  46.     if(m_mode == AUTO_SIZE)  
  47.     {  
  48.         setFixedSize(m_pixmap.size() * m_scale);  
  49.     }  
  50.     update();  
  51.     return true;  
  52. }  
  53.   
  54. void PictureBox::paintEvent(QPaintEvent * event)  
  55. {  
  56.     Q_UNUSED(event);  
  57.     QPainter painter(this);  
  58.     painter.setBackground(m_brush);  
  59.     painter.eraseRect(rect());  
  60.   
  61.     double window_width, window_height;  
  62.     double image_width, image_height;  
  63.     double r1, r2, r;  
  64.     int offset_x, offset_y;  
  65.     switch (m_mode)  
  66.     {  
  67.     case FIXED_SIZE:  
  68.     case AUTO_SIZE:  
  69.         painter.scale(m_scale, m_scale);  
  70.         painter.drawPixmap(0, 0, m_pixmap);  
  71.         break;  
  72.     case FIX_SIZE_CENTRED:  
  73.         window_width = width();  
  74.         window_height = height();  
  75.         image_width = m_pixmap.width();  
  76.         image_height = m_pixmap.height();  
  77.         offset_x = (window_width - m_scale * image_width) / 2;  
  78.         offset_y = (window_height - m_scale * image_height) / 2;  
  79.         painter.translate(offset_x, offset_y);  
  80.         painter.scale(m_scale, m_scale);  
  81.         painter.drawPixmap(0, 0, m_pixmap);  
  82.         break;  
  83.     case AUTO_ZOOM:  
  84.         window_width = width();  
  85.         window_height = height();  
  86.         image_width = m_pixmap.width();  
  87.         image_height = m_pixmap.height();  
  88.         r1 = window_width / image_width;  
  89.         r2 = window_height / image_height;  
  90.         r = qMin(r1, r2);  
  91.         offset_x = (window_width - r * image_width) / 2;  
  92.         offset_y = (window_height - r * image_height) / 2;  
  93.         painter.translate(offset_x, offset_y);  
  94.         painter.scale(r, r);  
  95.         painter.drawPixmap(0, 0, m_pixmap);  
  96.         break;  
  97.     }  
  98. }  
  99.   
  100. PictureBox::~PictureBox()  
  101. {  
  102.   
  103. }  

 

AUTO_ZOOM 模式下顯示例子如下:

 

FIXED_SIZE 模式下的例子如下:

 

FIX_SIZE_CENTRED 模式下的例子如下:

 

http://blog.csdn.net/liyuanbhu/article/details/46687495


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM