參考資料: Image Viewer Example
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
自己照著上面範例練習改成Qt5版本, 並支援繁體中文喔!
放大圖片功能
影像縮小功能
原始圖大小
當視窗的尺寸大於圖片, 圖片會自動伸展成一樣大小,即stretch功能
//----------------------------------------------------------------------------------------------------------------------------------------------------------
imageview.h
#ifndef IMAGEVIEWER_H
#define IMAGEVIEWER_H
#include <QMainWindow>
#include <QPrinter>
// #include <QLabel>
// #include <QScrollArea>
// #include <QScrollBar>
// #include <QFileDialog>
// #include <QtGui>
// #include <QMessageBox>
// #include <QAction>
// #include <QPrintDialog>
// #include <QMenu>
// #include <QMenuBar>
class QAction;
class QLabel;
class QMenu;
class QScrollArea;
class QScrollBar;
class ImageViewer : public QMainWindow
{
Q_OBJECT // 若缺少會顯示 Class declarations lacks Q_OBJECT marco
public:
ImageViewer(); // 預設constructor
private slots: // 以下為Callback function宣告(事件處理用)
void open();
void print();
void zoomIn();
void zoomOut();
void normalSize();
void fitToWindow();
private: // 以下為副程式宣告
void createActions();
void createMenus();
void updateActions();
void scaleImage(double factor);
void adjustScrollBar(QScrollBar *scrollBar, double factor);
QLabel *imageLabel; // 顯示圖片用
QScrollArea *scrollArea; // 用來產生scrollbar
double scaleFactor; // 縮放比例
#ifndef QT_NO_PRINTER
QPrinter printer;
#endif
// 以下為事件觸發sender
QAction *openAct;
QAction *printAct;
QAction *exitAct;
QAction *zoomInAct;
QAction *zoomOutAct;
QAction *normalSizeAct;
QAction *fitToWindowAct;
// 以下為選單
QMenu *fileMenu;
QMenu *viewMenu;
//QMenu *helpMenu;
};
#endif
//----------------------------------------------------------------------------------------------------------------------------------------------------------
imageview.cpp
#include "imageviewer.h"
#pragma execution_character_set("utf-8") // 繁體中文才不會顯示成亂碼
ImageViewer::ImageViewer()
{
imageLabel = new QLabel; // QLabel用來顯示影像
//imageLabel->setBackgroundRole(QPalette::Base);
//imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true); /* stretched 效果,
*如果影像解析度超過畫面範圍會自動縮放, 而不會被砍掉*/
scrollArea = new QScrollArea;
//scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel); // 設定imageLabel為scrollArea的child
setCentralWidget(scrollArea); // 設定scrollArea在main Window中間位置
createActions(); // 建立Action事件處理
createMenus(); // 建立Menu: 建立Menu與Action事件處理的關聯
QString title = "影像瀏覽器";
setWindowTitle(title); // 視窗title
resize(800, 600); // 視窗大小
}
//--------------------------------------------------------------------------
void ImageViewer::open()
{
QString fileName = QFileDialog::getOpenFileName(this,
"Open File", QDir::currentPath());
if (!fileName.isEmpty()) {
QImage image(fileName); // 載入影像
if (image.isNull()) { // 如果無法載入則image為空值(Null)
QMessageBox::information(this, "影像瀏覽器",
tr("無法載入 %1.").arg(fileName));
return;
}
setWindowTitle(fileName); // 顯示檔案路徑
imageLabel->setPixmap(QPixmap::fromImage(image)); // image顯示在imageLabel上
scaleFactor = 1.0;
printAct->setEnabled(true); // printAct致能
fitToWindowAct->setEnabled(true); // fitToWindowAct致能
updateActions();
if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
}
}
//-------------------------------------------------------------
void ImageViewer::print()
{
Q_ASSERT(imageLabel->pixmap());
#ifndef QT_NO_PRINTER
QPrintDialog dialog(&printer, this);
if (dialog.exec()) {
QPainter painter(&printer);
QRect rect = painter.viewport();
QSize size = imageLabel->pixmap()->size();
size.scale(rect.size(), Qt::KeepAspectRatio);
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
painter.setWindow(imageLabel->pixmap()->rect());
painter.drawPixmap(0, 0, *imageLabel->pixmap());
}
#endif
}
//-------------------------------------------------------------
void ImageViewer::zoomIn()
{
scaleImage(1.25);
}
//-------------------------------------------------------------
void ImageViewer::zoomOut()
{
scaleImage(0.8);
}
//-------------------------------------------------------------
void ImageViewer::normalSize()
{
imageLabel->adjustSize();
scaleFactor = 1.0;
}
//-------------------------------------------------------------
void ImageViewer::fitToWindow()
{
bool fitToWindow = fitToWindowAct->isChecked(); //判定是否勾選 fitToWindow
scrollArea->setWidgetResizable(fitToWindow);
if (!fitToWindow) {
normalSize();
}
updateActions();
}
//-------------------------------------------------------------
void ImageViewer::createActions()
{
//---------------------------------------------------------
openAct = new QAction("&Open...", this); // 開啟事件
openAct->setShortcut(tr("Ctrl+O")); // 建立快捷鍵
//connect(觸發sender,訊號種類,觸發receiver, Callback function)
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
//---------------------------------------------------------
printAct = new QAction("&Print...", this);
printAct->setShortcut(tr("Ctrl+P"));
printAct->setEnabled(false);
connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
//---------------------------------------------------------
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
//---------------------------------------------------------
zoomInAct = new QAction("Zoom &In (25%)", this);
zoomInAct->setShortcut(tr("Ctrl++"));
zoomInAct->setEnabled(false);
connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
//---------------------------------------------------------
zoomOutAct = new QAction("Zoom &Out (25%)", this);
zoomOutAct->setShortcut(tr("Ctrl+-"));
zoomOutAct->setEnabled(false);
connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
//---------------------------------------------------------
normalSizeAct = new QAction("&Normal Size", this);
normalSizeAct->setShortcut(tr("Ctrl+S"));
normalSizeAct->setEnabled(false);
connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
//---------------------------------------------------------
fitToWindowAct = new QAction("&Fit to Window", this);
fitToWindowAct->setEnabled(false);
fitToWindowAct->setCheckable(true);
fitToWindowAct->setShortcut(tr("Ctrl+F"));
connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
}
//-------------------------------------------------------------
void ImageViewer::createMenus()
{
fileMenu = new QMenu("檔案", this); // 新增 fileMenu
fileMenu->addAction(openAct); // 加入openAct事件處理
fileMenu->addAction(printAct); // 加入printAct事件處理
fileMenu->addSeparator(); // 加入分隔線
fileMenu->addAction(exitAct); // 加入exit事件處理
viewMenu = new QMenu("瀏覽", this); // 新增 viewMenu
viewMenu->addAction(zoomInAct); // 新增zoomInAct事件處理
viewMenu->addAction(zoomOutAct); // 新增zoomOutAct事件處理
viewMenu->addAction(normalSizeAct); // 新增normalSizeAct事件處理
viewMenu->addSeparator(); // 加入分隔線
viewMenu->addAction(fitToWindowAct); // 新增fitToWindowAct事件處理
menuBar()->addMenu(fileMenu); // 加入fileMenu選單
menuBar()->addMenu(viewMenu); // 加入viewMenu選單
}
//-------------------------------------------------------------
void ImageViewer::updateActions()
{
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
}
//-------------------------------------------------------------
void ImageViewer::scaleImage(double factor)
{
Q_ASSERT(imageLabel->pixmap());
scaleFactor *= factor;
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
adjustScrollBar(scrollArea->verticalScrollBar(), factor);
zoomInAct->setEnabled(scaleFactor < 3.0);
zoomOutAct->setEnabled(scaleFactor > 0.333);
}
//-------------------------------------------------------------
void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
{
scrollBar->setValue(int(factor * scrollBar->value()
+ ((factor - 1) * scrollBar->pageStep()/2)));
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
imageview.pro
#include "imageviewer.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ImageViewer imageViewer;
imageViewer.show();
return app.exec();
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
imageviewer.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4) {
QT += printsupport
}
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
HEADERS = imageviewer.h
SOURCES = imageviewer.cpp \
main.cpp
# install
TARGET = imageviewer
TEMPLATE = app
留言列表