新建Qt项目,选择Qt Widgets Application,填入项目名称“ImageView”,点击完成。
在Qt Designer里会生成如图所示的几个文件:
此时我们右键删除imageviewer.ui这个文件,因为我们本次是用纯代码的方式生成界面,所以不需要这个ui文件了。
关键代码主要在imageviewer.h和imageviewer.cpp里。下面是代码:
imageviewer.h
1 #ifndef IMAGEVIEWER_H
2 #define IMAGEVIEWER_H
3
4 #include <QMainWindow>
5 #include <QLabel>
6 #include <QScrollArea>
7 #include <QMenu>
8 #include <QMenuBar>
9 #include <QToolBar>
10
11 namespace Ui {
12 class ImageViewer;
13 }
14
15 class ImageViewer : public QMainWindow
16 {
17 Q_OBJECT
18
19 public:
20 explicit ImageViewer(QWidget *parent = nullptr);
21 ~ImageViewer();
22
23 void initMenu();
24 void initToolBar();
25 void initConnect();
26
27 private:
28 Ui::ImageViewer *ui;
29 QLabel *imageLabel;
30 QScrollArea *scrollAera;
31
32 QMenu *fileMenu;
33 QMenu *viewMenu;
34 QMenu *helpMenu;
35
36 QToolBar *fileToolBar;
37
38 QAction *openAct;
39 QAction *printAct;
40 QAction *exitAct;
41 QAction *zoomInAct;
42 QAction *zoomOutAct;
43 QAction *normalSizeAct;
44 QAction *fitToWindowAct;
45 QAction *aboutAct;
46 QAction *aboutQtAct;
47
48 private slots:
49 void open();
50 void print();
51 void exit();
52 void zoomIn();
53 void zoomOut();
54 void normalSize();
55 void fitToWindow();
56 void about();
57 void aboutQt();
58 };
59
60 #endif // IMAGEVIEWER_H
imageviewer.cpp
1 #include "imageviewer.h"
2 #include "ui_imageviewer.h"
3
4 ImageViewer::ImageViewer(QWidget *parent) :
5 QMainWindow(parent),
6 ui(new Ui::ImageViewer)
7 {
8 ui->setupUi(this);
9
10 //initalize
11 initMenu();
12 initToolBar();
13 initConnect();
14 }
15
16 ImageViewer::~ImageViewer()
17 {
18 delete ui;
19 }
20
21 void ImageViewer::initMenu()
22 {
23 //QMenuBar *menuBar = this->menuBar();
24 //add three menus
25 fileMenu = new QMenu(tr("&File"),this);
26 viewMenu = new QMenu(tr("&View"),this);
27 helpMenu = new QMenu(tr("&About"),this);
28
29 //add actions and add it into corresponding menu
30 openAct = new QAction(tr("&Open"),this);
31 openAct -> setShortcut(tr("ctrl+O"));
32 printAct = new QAction(tr("&Print"),this);
33 printAct -> setShortcut(tr("ctrl+P"));
34 exitAct = new QAction(tr("&Exit"),this);
35 exitAct -> setShortcut(tr("ctrl+Q"));
36 fileMenu -> addAction(openAct);
37 fileMenu -> addAction(printAct);
38 fileMenu -> addSeparator();
39 fileMenu -> addAction(exitAct);
40
41 zoomInAct = new QAction(tr("Zoom &In"),this);
42 zoomInAct -> setShortcut(tr("ctrl+="));
43 zoomOutAct = new QAction(tr("Zoom &Out"),this);
44 zoomOutAct -> setShortcut(tr("ctrl+-"));
45 normalSizeAct = new QAction(tr("&Normal Size"),this);
46 normalSizeAct -> setShortcut(tr("ctrl+S"));
47 fitToWindowAct = new QAction(tr("&Fit to Window"),this);
48 fitToWindowAct -> setShortcut(tr("ctrl+F"));
49 viewMenu -> addAction(zoomInAct);
50 viewMenu -> addAction(zoomOutAct);
51 viewMenu -> addAction(normalSizeAct);
52 viewMenu -> addSeparator();
53 viewMenu -> addAction(fitToWindowAct);
54
55 aboutAct = new QAction(tr("&About"),this);
56 aboutQtAct = new QAction(tr("&About Qt"),this);
57 helpMenu -> addAction(aboutAct);
58 helpMenu -> addAction(aboutQtAct);
59
60 //add menus to menubar
61 menuBar() -> addMenu(fileMenu);
62 menuBar() -> addMenu(viewMenu);
63 menuBar() -> addMenu(helpMenu);
64 }
65
66 void ImageViewer::initToolBar()
67 {
68 //add a toolbar and add its actions
69 fileToolBar = new QToolBar(this);
70
71 fileToolBar -> addAction(openAct);
72 fileToolBar -> addAction(printAct);
73 fileToolBar -> addAction(exitAct);
74
75 addToolBar(Qt::TopToolBarArea,fileToolBar);
76 }
77
78 void ImageViewer::initConnect()
79 {
80 //singals and slots
81 connect(openAct,SIGNAL(triggered),this,SLOT(open()));
82 connect(printAct,SIGNAL(triggered),this,SLOT(print()));
83 connect(exitAct,SIGNAL(triggered),this,SLOT(exit()));
84 connect(zoomInAct,SIGNAL(triggered),this,SLOT(zoomIn()));
85 connect(zoomOutAct,SIGNAL(triggered),this,SLOT(zoomOut()));
86 connect(normalSizeAct,SIGNAL(triggered),this,SLOT(normalSize()));
87 connect(fitToWindowAct,SIGNAL(triggered),this,SLOT(fitToWindow()));
88 connect(aboutAct,SIGNAL(triggered),this,SLOT(about()));
89 connect(aboutQtAct,SIGNAL(triggered),this,SLOT(aboutQt()));
90 }
91
92 //implement slot functions
93 void ImageViewer::open()
94 {
95
96 }
97
98 void ImageViewer::print()
99 {
100
101 }
102
103 void ImageViewer::exit()
104 {
105
106 }
107
108 void ImageViewer::zoomIn()
109 {
110
111 }
112
113 void ImageViewer::zoomOut()
114 {
115
116 }
117
118 void ImageViewer::normalSize()
119 {
120
121 }
122
123 void ImageViewer::fitToWindow()
124 {
125
126 }
127
128 void ImageViewer::about()
129 {
130
131 }
132
133 void ImageViewer::aboutQt()
134 {
135
136 }
界面效果如图: