Learn C++ Programming Language
This project is maintained by cugwhp
本次实习的目标是掌握Qt的IDE开发环境、Qt开发的基本框架,学习Qt示例工程imageviewer,掌握GUI程序的基本要素:视图、菜单、工具栏等。
Qt为VS安装了插件之后,VS菜单会出现Qt菜单项,界面如下:
QtCreator是类似VS的IDE开发环境。
示例代码界面图
// imageviewer.cpp - imageviewer 实现
// imageviewer.h - imageviewer 头文件
// imageviewer.pro - 工程文件
// main.cpp - 主程序main
// main.cpp 是C++程序的唯一入口,定义了一个Qapplication对象,直至程序结束时返回。
#include <QApplication> //Qt Application
#include <QCommandLineParser> //Qt CommandLineParser, parse argv[]
#include "imageviewer.h" //class imageviewer declartion
int main(int argc, char *argv[]) //main function
{
QApplication app(argc, argv); //application
QGuiApplication::setApplicationDisplayName(ImageViewer::tr("Image Viewer")); //Set Application Title
// 可执行程序是可以带有参数调用的,如:imageviewer.exe d:/test.img 中的
// d:/test.img 会存储在argv[1]中.
QCommandLineParser commandLineParser;
commandLineParser.addHelpOption();
commandLineParser.addPositionalArgument(ImageViewer::tr("[file]"), ImageViewer::tr("Image file to open.")); //bind the 1st parameters with open
commandLineParser.process(QCoreApplication::arguments());
ImageViewer imageViewer; // imageviewer window to display one image
// 如果程序带有参数,则选择调用打开文件
if (!commandLineParser.positionalArguments().isEmpty()
&& !imageViewer.loadFile(commandLineParser.positionalArguments().front())) {
return -1;
}
imageViewer.show(); //显示窗口
return app.exec(); //运行程序,直到程序退出
//思考:程序是如何做到一直运行的呢??
}
#ifndef IMAGEVIEWER_H
#define IMAGEVIEWER_H
#include <QMainWindow>
#include <QImage>
#ifndef QT_NO_PRINTER
#include <QPrinter>
#endif
QT_BEGIN_NAMESPACE
class QAction;
class QLabel;
class QMenu;
class QScrollArea;
class QScrollBar;
QT_END_NAMESPACE
//! [0]
class ImageViewer : public QMainWindow
{
Q_OBJECT
public:
ImageViewer(); //构造函数
bool loadFile(const QString &); //加载图像
//信号槽
private slots:
void open();
void saveAs();
void print();
void copy();
void paste();
void zoomIn();
void zoomOut();
void normalSize();
void fitToWindow();
void about();
private:
void createActions();
void createMenus();
void updateActions();
bool saveFile(const QString &fileName);
void setImage(const QImage &newImage);
void scaleImage(double factor);
void adjustScrollBar(QScrollBar *scrollBar, double factor);
QImage image; //用于绘制的图像对象,类似BITMAP
QLabel *imageLabel; //绘制图像的控件载体,类似Picture
QScrollArea *scrollArea; //滚动条
double scaleFactor; //缩放比例
#ifndef QT_NO_PRINTER
QPrinter printer;
#endif
QAction *saveAsAct;
QAction *printAct;
QAction *copyAct;
QAction *zoomInAct;
QAction *zoomOutAct;
QAction *normalSizeAct;
QAction *fitToWindowAct;
};
//! [0]
#endif
QT += widgets
requires(qtConfig(filedialog))
qtHaveModule(printsupport): QT += printsupport
HEADERS = imageviewer.h
SOURCES = imageviewer.cpp \
main.cpp
# install
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/widgets/imageviewer
INSTALLS += target
参见Qt Image Viewer Example code download