头文件:
1 class CImagePreviewerCtrl : public QWidget
2 {
3 Q_OBJECT
4
5 public:
6 CImagePreviewerCtrl(QWidget *parent = 0);
7 ~CImagePreviewerCtrl();
8
9 void SetImageFile(const QString& fileName);
10
11 virtual void paintEvent(QPaintEvent *event) override;
12 private:
13 QImage m_Image;
14 QString m_FileName;
15 };
16
CPP文件:
1 CImagePreviewerCtrl::CImagePreviewerCtrl(QWidget *parent)
2 : QWidget(parent)
3 {
4
5 }
6
7 CImagePreviewerCtrl::~CImagePreviewerCtrl()
8 {
9
10 }
11
12 void CImagePreviewerCtrl::SetImageFile(const QString& fileName)
13 {
14 m_FileName = fileName;
15 m_Image.load(fileName);
16 update();
17 }
18
19
20 void CImagePreviewerCtrl::paintEvent(QPaintEvent *event)
21 {
22 if(m_Image.isNull())
23 return;
24
25 QPainter newPainter(this);
26 newPainter.drawImage(this->rect(), m_Image);
27 }