Qt 中沒有專門顯示圖片的控件,通常我們會使用QLabel來顯示圖片。但是QLabel 顯示圖片的能力還是有點弱。比如不支持圖像的縮放一類的功能,使用起來不是很方便。因此我就自己寫了個簡單的類。
我這個類支持三種圖像顯示模式,我分別稱之為:FIXED_SIZE, CENTRED,AUTO_ZOOM, AUTO_SIZE。
- FIXED_SIZE 模式下,顯示的圖像大小等於圖像尺寸乘以縮放因子,如果控件的尺寸小於這個大小則多出的部分被裁切掉。
- FIX_SIZE_CENTRED模式與FIXED_SIZE 模式類似,但是,顯示的圖像居於窗口正中。
- AUTO_ZOOM 模式下,顯示的圖像大小自動適應控件窗口大小。
- AUTO_SIZE 模式下,這個控件的尺寸等於圖像尺寸乘以縮放因子。
下面是代碼:
- #ifndef PICTUREBOX_H
- #define PICTUREBOX_H
- #include <QWidget>
- #include <QImage>
- #include <QPixmap>
- class PictureBox : public QWidget
- {
- Q_OBJECT
- public:
- enum PB_MODE {FIXED_SIZE, FIX_SIZE_CENTRED, AUTO_ZOOM, AUTO_SIZE};
- explicit PictureBox(QWidget *parent = 0);
- void setMode(PB_MODE mode);
- ~PictureBox();
- private:
- QPixmap m_pixmap;
- double m_scale;
- PB_MODE m_mode;
- QBrush m_brush;
- protected:
- void paintEvent(QPaintEvent * event);
- signals:
- public slots:
- bool setImage(QImage &image, double scale = 1.0);
- void setBackground(QBrush brush);
- };
- #endif // PICTUREBOX_H
- #include "picturebox.h"
- #include <QPainter>
- #include <QDebug>
- static const int IMAGE_WIDTH = 160;
- static const int IMAGE_HEIGHT = 120;
- static const QSize IMAGE_SIZE = QSize(IMAGE_WIDTH, IMAGE_HEIGHT);
- PictureBox::PictureBox(QWidget *parent) : QWidget(parent)
- {
- m_pixmap = QPixmap(IMAGE_SIZE);
- m_pixmap.fill();
- m_scale = 1.0;
- m_mode = FIXED_SIZE;
- m_brush = QBrush(Qt::white);
- }
- void PictureBox::setBackground(QBrush brush)
- {
- m_brush = brush;
- update();
- }
- void PictureBox::setMode(PB_MODE mode)
- {
- m_mode = mode;
- if(m_mode == AUTO_SIZE)
- {
- setFixedSize(m_pixmap.size() * m_scale);
- }
- else
- {
- setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
- setMinimumSize(0, 0);
- }
- update();
- }
- bool PictureBox::setImage(QImage &image, double scale)
- {
- if(image.isNull())
- {
- return false;
- }
- m_pixmap = QPixmap::fromImage(image);
- m_scale = qBound(0.01, scale, 100.0);
- if(m_mode == AUTO_SIZE)
- {
- setFixedSize(m_pixmap.size() * m_scale);
- }
- update();
- return true;
- }
- void PictureBox::paintEvent(QPaintEvent * event)
- {
- Q_UNUSED(event);
- QPainter painter(this);
- painter.setBackground(m_brush);
- painter.eraseRect(rect());
- double window_width, window_height;
- double image_width, image_height;
- double r1, r2, r;
- int offset_x, offset_y;
- switch (m_mode)
- {
- case FIXED_SIZE:
- case AUTO_SIZE:
- painter.scale(m_scale, m_scale);
- painter.drawPixmap(0, 0, m_pixmap);
- break;
- case FIX_SIZE_CENTRED:
- window_width = width();
- window_height = height();
- image_width = m_pixmap.width();
- image_height = m_pixmap.height();
- offset_x = (window_width - m_scale * image_width) / 2;
- offset_y = (window_height - m_scale * image_height) / 2;
- painter.translate(offset_x, offset_y);
- painter.scale(m_scale, m_scale);
- painter.drawPixmap(0, 0, m_pixmap);
- break;
- case AUTO_ZOOM:
- window_width = width();
- window_height = height();
- image_width = m_pixmap.width();
- image_height = m_pixmap.height();
- r1 = window_width / image_width;
- r2 = window_height / image_height;
- r = qMin(r1, r2);
- offset_x = (window_width - r * image_width) / 2;
- offset_y = (window_height - r * image_height) / 2;
- painter.translate(offset_x, offset_y);
- painter.scale(r, r);
- painter.drawPixmap(0, 0, m_pixmap);
- break;
- }
- }
- PictureBox::~PictureBox()
- {
- }
AUTO_ZOOM 模式下顯示例子如下:
FIXED_SIZE 模式下的例子如下:
FIX_SIZE_CENTRED 模式下的例子如下:
http://blog.csdn.net/liyuanbhu/article/details/46687495