3
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.1: imageviewer.cpp Example File (widgets/imageviewer/imageviewer.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
6
<title>Qt 4.2: imageviewer.cpp Example File (widgets/imageviewer/imageviewer.cpp)</title>
7
<link href="classic.css" rel="stylesheet" type="text/css" />
15
10
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
12
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
18
13
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">imageviewer.cpp Example File<br /><sup><sup>widgets/imageviewer/imageviewer.cpp</sup></sup></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be used under the terms of the GNU General Public
27
** License version 2.0 as published by the Free Software Foundation
28
** and appearing in the file LICENSE.GPL included in the packaging of
29
** this file. Please review the following information to ensure GNU
30
** General Public Licensing requirements will be met:
31
** http://www.trolltech.com/products/qt/opensource.html
33
** If you are unsure which license is appropriate for your use, please
34
** review the following information:
35
** http://www.trolltech.com/products/qt/licensing.html or contact the
36
** sales department at sales@trolltech.com.
38
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
39
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
41
****************************************************************************/
43
#include <QtGui>
45
#include "imageviewer.h"
47
ImageViewer::ImageViewer()
49
imageLabel = new QLabel;
50
imageLabel->setBackgroundRole(QPalette::Base);
51
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
52
imageLabel->setScaledContents(true);
54
scrollArea = new QScrollArea;
55
scrollArea->setBackgroundRole(QPalette::Dark);
56
scrollArea->setWidget(imageLabel);
57
setCentralWidget(scrollArea);
62
setWindowTitle(tr("Image Viewer"));
66
void ImageViewer::open()
68
QString fileName = QFileDialog::getOpenFileName(this,
69
tr("Open File"), QDir::currentPath());
70
if (!fileName.isEmpty()) {
71
QImage image(fileName);
73
QMessageBox::information(this, tr("Image Viewer"),
74
tr("Cannot load %1.").arg(fileName));
77
imageLabel->setPixmap(QPixmap::fromImage(image));
80
printAct->setEnabled(true);
81
fitToWindowAct->setEnabled(true);
84
if (!fitToWindowAct->isChecked())
85
imageLabel->adjustSize();
89
void ImageViewer::print()
91
Q_ASSERT(imageLabel->pixmap());
92
QPrintDialog dialog(&printer, this);
94
QPainter painter(&printer);
95
QRect rect = painter.viewport();
96
QSize size = imageLabel->pixmap()->size();
97
size.scale(rect.size(), Qt::KeepAspectRatio);
98
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
99
painter.setWindow(imageLabel->pixmap()->rect());
100
painter.drawPixmap(0, 0, *imageLabel->pixmap());
104
void ImageViewer::zoomIn()
109
void ImageViewer::zoomOut()
114
void ImageViewer::normalSize()
116
imageLabel->adjustSize();
120
void ImageViewer::fitToWindow()
122
bool fitToWindow = fitToWindowAct->isChecked();
123
scrollArea->setWidgetResizable(fitToWindow);
130
void ImageViewer::about()
132
QMessageBox::about(this, tr("About Image Viewer"),
133
tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
134
"and QScrollArea to display an image. QLabel is typically used "
135
"for displaying a text, but it can also display an image. "
136
"QScrollArea provides a scrolling view around another widget. "
137
"If the child widget exceeds the size of the frame, QScrollArea "
138
"automatically provides scroll bars. </p><p>The example "
139
"demonstrates how QLabel's ability to scale its contents "
140
"(QLabel::scaledContents), and QScrollArea's ability to "
141
"automatically resize its contents "
142
"(QScrollArea::widgetResizable), can be used to implement "
143
"zooming and scaling features. </p><p>In addition the example "
144
"shows how to use QPainter to print an image.</p>"));
147
void ImageViewer::createActions()
149
openAct = new QAction(tr("&Open..."), this);
150
openAct->setShortcut(tr("Ctrl+O"));
151
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
153
printAct = new QAction(tr("&Print..."), this);
154
printAct->setShortcut(tr("Ctrl+P"));
155
printAct->setEnabled(false);
156
connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
158
exitAct = new QAction(tr("E&xit"), this);
159
exitAct->setShortcut(tr("Ctrl+Q"));
160
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
162
zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
163
zoomInAct->setShortcut(tr("Ctrl++"));
164
zoomInAct->setEnabled(false);
165
connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
167
zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
168
zoomOutAct->setShortcut(tr("Ctrl+-"));
169
zoomOutAct->setEnabled(false);
170
connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
172
normalSizeAct = new QAction(tr("&Normal Size"), this);
173
normalSizeAct->setShortcut(tr("Ctrl+S"));
174
normalSizeAct->setEnabled(false);
175
connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
177
fitToWindowAct = new QAction(tr("&Fit to Window"), this);
178
fitToWindowAct->setEnabled(false);
179
fitToWindowAct->setCheckable(true);
180
fitToWindowAct->setShortcut(tr("Ctrl+F"));
181
connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
183
aboutAct = new QAction(tr("&About"), this);
184
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
186
aboutQtAct = new QAction(tr("About &Qt"), this);
187
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
190
void ImageViewer::createMenus()
192
fileMenu = new QMenu(tr("&File"), this);
193
fileMenu->addAction(openAct);
194
fileMenu->addAction(printAct);
195
fileMenu->addSeparator();
196
fileMenu->addAction(exitAct);
198
viewMenu = new QMenu(tr("&View"), this);
199
viewMenu->addAction(zoomInAct);
200
viewMenu->addAction(zoomOutAct);
201
viewMenu->addAction(normalSizeAct);
202
viewMenu->addSeparator();
203
viewMenu->addAction(fitToWindowAct);
205
helpMenu = new QMenu(tr("&Help"), this);
206
helpMenu->addAction(aboutAct);
207
helpMenu->addAction(aboutQtAct);
209
menuBar()->addMenu(fileMenu);
210
menuBar()->addMenu(viewMenu);
211
menuBar()->addMenu(helpMenu);
214
void ImageViewer::updateActions()
216
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
217
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
218
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
221
void ImageViewer::scaleImage(double factor)
223
Q_ASSERT(imageLabel->pixmap());
224
scaleFactor *= factor;
225
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
227
adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
228
adjustScrollBar(scrollArea->verticalScrollBar(), factor);
230
zoomInAct->setEnabled(scaleFactor < 3.0);
231
zoomOutAct->setEnabled(scaleFactor > 0.333);
234
void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
236
scrollBar->setValue(int(factor * scrollBar->value()
237
+ ((factor - 1) * scrollBar->pageStep()/2)));
14
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">imageviewer.cpp Example File<br /><sup><sup>widgets/imageviewer/imageviewer.cpp</sup></sup></h1>
15
<pre> /****************************************************************************
17
** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
19
** This file is part of the example classes of the Qt Toolkit.
21
** This file may be used under the terms of the GNU General Public
22
** License version 2.0 as published by the Free Software Foundation
23
** and appearing in the file LICENSE.GPL included in the packaging of
24
** this file. Please review the following information to ensure GNU
25
** General Public Licensing requirements will be met:
26
** http:<span class="comment">//www.trolltech.com/products/qt/opensource.html</span>
28
** If you are unsure which license is appropriate for your use, please
29
** review the following information:
30
** http:<span class="comment">//www.trolltech.com/products/qt/licensing.html or contact the</span>
31
** sales department at sales@trolltech.com.
33
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
34
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
36
****************************************************************************/
38
#include <QtGui>
40
#include "imageviewer.h"
42
ImageViewer::ImageViewer()
44
imageLabel = new QLabel;
45
imageLabel->setBackgroundRole(QPalette::Base);
46
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
47
imageLabel->setScaledContents(true);
49
scrollArea = new QScrollArea;
50
scrollArea->setBackgroundRole(QPalette::Dark);
51
scrollArea->setWidget(imageLabel);
52
setCentralWidget(scrollArea);
57
setWindowTitle(tr("Image Viewer"));
61
void ImageViewer::open()
63
QString fileName = QFileDialog::getOpenFileName(this,
64
tr("Open File"), QDir::currentPath());
65
if (!fileName.isEmpty()) {
66
QImage image(fileName);
68
QMessageBox::information(this, tr("Image Viewer"),
69
tr("Cannot load %1.").arg(fileName));
72
imageLabel->setPixmap(QPixmap::fromImage(image));
75
printAct->setEnabled(true);
76
fitToWindowAct->setEnabled(true);
79
if (!fitToWindowAct->isChecked())
80
imageLabel->adjustSize();
84
void ImageViewer::print()
86
Q_ASSERT(imageLabel->pixmap());
87
QPrintDialog dialog(&printer, this);
89
QPainter painter(&printer);
90
QRect rect = painter.viewport();
91
QSize size = imageLabel->pixmap()->size();
92
size.scale(rect.size(), Qt::KeepAspectRatio);
93
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
94
painter.setWindow(imageLabel->pixmap()->rect());
95
painter.drawPixmap(0, 0, *imageLabel->pixmap());
99
void ImageViewer::zoomIn()
104
void ImageViewer::zoomOut()
109
void ImageViewer::normalSize()
111
imageLabel->adjustSize();
115
void ImageViewer::fitToWindow()
117
bool fitToWindow = fitToWindowAct->isChecked();
118
scrollArea->setWidgetResizable(fitToWindow);
125
void ImageViewer::about()
127
QMessageBox::about(this, tr("About Image Viewer"),
128
tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
129
"and QScrollArea to display an image. QLabel is typically used "
130
"for displaying a text, but it can also display an image. "
131
"QScrollArea provides a scrolling view around another widget. "
132
"If the child widget exceeds the size of the frame, QScrollArea "
133
"automatically provides scroll bars. </p><p>The example "
134
"demonstrates how QLabel's ability to scale its contents "
135
"(QLabel::scaledContents), and QScrollArea's ability to "
136
"automatically resize its contents "
137
"(QScrollArea::widgetResizable), can be used to implement "
138
"zooming and scaling features. </p><p>In addition the example "
139
"shows how to use QPainter to print an image.</p>"));
142
void ImageViewer::createActions()
144
openAct = new QAction(tr("&Open..."), this);
145
openAct->setShortcut(tr("Ctrl+O"));
146
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
148
printAct = new QAction(tr("&Print..."), this);
149
printAct->setShortcut(tr("Ctrl+P"));
150
printAct->setEnabled(false);
151
connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
153
exitAct = new QAction(tr("E&xit"), this);
154
exitAct->setShortcut(tr("Ctrl+Q"));
155
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
157
zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
158
zoomInAct->setShortcut(tr("Ctrl++"));
159
zoomInAct->setEnabled(false);
160
connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
162
zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
163
zoomOutAct->setShortcut(tr("Ctrl+-"));
164
zoomOutAct->setEnabled(false);
165
connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
167
normalSizeAct = new QAction(tr("&Normal Size"), this);
168
normalSizeAct->setShortcut(tr("Ctrl+S"));
169
normalSizeAct->setEnabled(false);
170
connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
172
fitToWindowAct = new QAction(tr("&Fit to Window"), this);
173
fitToWindowAct->setEnabled(false);
174
fitToWindowAct->setCheckable(true);
175
fitToWindowAct->setShortcut(tr("Ctrl+F"));
176
connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
178
aboutAct = new QAction(tr("&About"), this);
179
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
181
aboutQtAct = new QAction(tr("About &Qt"), this);
182
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
185
void ImageViewer::createMenus()
187
fileMenu = new QMenu(tr("&File"), this);
188
fileMenu->addAction(openAct);
189
fileMenu->addAction(printAct);
190
fileMenu->addSeparator();
191
fileMenu->addAction(exitAct);
193
viewMenu = new QMenu(tr("&View"), this);
194
viewMenu->addAction(zoomInAct);
195
viewMenu->addAction(zoomOutAct);
196
viewMenu->addAction(normalSizeAct);
197
viewMenu->addSeparator();
198
viewMenu->addAction(fitToWindowAct);
200
helpMenu = new QMenu(tr("&Help"), this);
201
helpMenu->addAction(aboutAct);
202
helpMenu->addAction(aboutQtAct);
204
menuBar()->addMenu(fileMenu);
205
menuBar()->addMenu(viewMenu);
206
menuBar()->addMenu(helpMenu);
209
void ImageViewer::updateActions()
211
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
212
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
213
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
216
void ImageViewer::scaleImage(double factor)
218
Q_ASSERT(imageLabel->pixmap());
219
scaleFactor *= factor;
220
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
222
adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
223
adjustScrollBar(scrollArea->verticalScrollBar(), factor);
225
zoomInAct->setEnabled(scaleFactor < 3.0);
226
zoomOutAct->setEnabled(scaleFactor > 0.333);
229
void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
231
scrollBar->setValue(int(factor * scrollBar->value()
232
+ ((factor - 1) * scrollBar->pageStep()/2)));
239
234
<p /><address><hr /><div align="center">
240
235
<table width="100%" cellspacing="0" border="0"><tr class="address">
241
236
<td width="30%">Copyright © 2006 <a href="trolltech.html">Trolltech</a></td>
242
237
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
243
<td width="30%" align="right"><div align="right">Qt 4.1.4</div></td>
238
<td width="30%" align="right"><div align="right">Qt 4.2.0</div></td>
244
239
</tr></table></div></address></body>