1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: mainwindow.cpp Example File (opengl/grabber/mainwindow.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>
15
<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>
18
<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="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</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">mainwindow.cpp Example File<br /><small><small>opengl/grabber/mainwindow.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be distributed under the terms of the Q Public License
27
** as defined by Trolltech AS of Norway and appearing in the file
28
** LICENSE.QPL included in the packaging of this file.
30
** This file may be distributed and/or modified under the terms of the
31
** GNU General Public License version 2 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file.
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
36
** information about Qt Commercial License Agreements.
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
40
** Contact info@trolltech.com if any conditions of this licensing are
43
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
44
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
****************************************************************************/
48
#include <QtGui>
49
#include <QtOpenGL>
51
#include "glwidget.h"
52
#include "mainwindow.h"
54
MainWindow::MainWindow()
56
centralWidget = new QWidget;
57
setCentralWidget(centralWidget);
59
glWidget = new GLWidget;
60
pixmapLabel = new QLabel;
62
glWidgetArea = new QScrollArea;
63
glWidgetArea->setWidget(glWidget);
64
glWidgetArea->setWidgetResizable(true);
65
glWidgetArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66
glWidgetArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
67
glWidgetArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
68
glWidgetArea->setMinimumSize(50, 50);
70
pixmapLabelArea = new QScrollArea;
71
pixmapLabelArea->setWidget(pixmapLabel);
72
pixmapLabelArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
73
pixmapLabelArea->setMinimumSize(50, 50);
75
xSlider = createSlider(SIGNAL(xRotationChanged(int)),
76
SLOT(setXRotation(int)));
77
ySlider = createSlider(SIGNAL(yRotationChanged(int)),
78
SLOT(setYRotation(int)));
79
zSlider = createSlider(SIGNAL(zRotationChanged(int)),
80
SLOT(setZRotation(int)));
85
QGridLayout *centralLayout = new QGridLayout;
86
centralLayout->addWidget(glWidgetArea, 0, 0);
87
centralLayout->addWidget(pixmapLabelArea, 0, 1);
88
centralLayout->addWidget(xSlider, 1, 0, 1, 2);
89
centralLayout->addWidget(ySlider, 2, 0, 1, 2);
90
centralLayout->addWidget(zSlider, 3, 0, 1, 2);
91
centralWidget->setLayout(centralLayout);
93
xSlider->setValue(15 * 16);
94
ySlider->setValue(345 * 16);
95
zSlider->setValue(0 * 16);
97
setWindowTitle(tr("Grabber"));
101
void MainWindow::renderIntoPixmap()
103
QSize size = getSize();
104
if (size.isValid()) {
105
QPixmap pixmap = glWidget->renderPixmap(size.width(), size.height());
110
void MainWindow::grabFrameBuffer()
112
QImage image = glWidget->grabFrameBuffer();
113
setPixmap(QPixmap::fromImage(image));
116
void MainWindow::clearPixmap()
118
setPixmap(QPixmap());
121
void MainWindow::about()
123
QMessageBox::about(this, tr("About Grabber"),
124
tr("The <b>Grabber</b> example demonstrates two approaches for "
125
"rendering OpenGL into a Qt pixmap."));
128
void MainWindow::createActions()
130
renderIntoPixmapAct = new QAction(tr("&Render into Pixmap..."), this);
131
renderIntoPixmapAct->setShortcut(tr("Ctrl+R"));
132
connect(renderIntoPixmapAct, SIGNAL(triggered()),
133
this, SLOT(renderIntoPixmap()));
135
grabFrameBufferAct = new QAction(tr("&Grab Frame Buffer"), this);
136
grabFrameBufferAct->setShortcut(tr("Ctrl+G"));
137
connect(grabFrameBufferAct, SIGNAL(triggered()),
138
this, SLOT(grabFrameBuffer()));
140
clearPixmapAct = new QAction(tr("&Clear Pixmap"), this);
141
clearPixmapAct->setShortcut(tr("Ctrl+L"));
142
connect(clearPixmapAct, SIGNAL(triggered()), this, SLOT(clearPixmap()));
144
exitAct = new QAction(tr("E&xit"), this);
145
exitAct->setShortcut(tr("Ctrl+Q"));
146
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
148
aboutAct = new QAction(tr("&About"), this);
149
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
151
aboutQtAct = new QAction(tr("About &Qt"), this);
152
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
155
void MainWindow::createMenus()
157
fileMenu = menuBar()->addMenu(tr("&File"));
158
fileMenu->addAction(renderIntoPixmapAct);
159
fileMenu->addAction(grabFrameBufferAct);
160
fileMenu->addAction(clearPixmapAct);
161
fileMenu->addSeparator();
162
fileMenu->addAction(exitAct);
164
helpMenu = menuBar()->addMenu(tr("&Help"));
165
helpMenu->addAction(aboutAct);
166
helpMenu->addAction(aboutQtAct);
169
QSlider *MainWindow::createSlider(const char *changedSignal,
170
const char *setterSlot)
172
QSlider *slider = new QSlider(Qt::Horizontal);
173
slider->setRange(0, 360 * 16);
174
slider->setSingleStep(16);
175
slider->setPageStep(15 * 16);
176
slider->setTickInterval(15 * 16);
177
slider->setTickPosition(QSlider::TicksRight);
178
connect(slider, SIGNAL(valueChanged(int)), glWidget, setterSlot);
179
connect(glWidget, changedSignal, slider, SLOT(setValue(int)));
183
void MainWindow::setPixmap(const QPixmap &pixmap)
185
pixmapLabel->setPixmap(pixmap);
186
QSize size = pixmap.size();
187
if (size - QSize(1, 0) == pixmapLabelArea->maximumViewportSize())
189
pixmapLabel->resize(size);
192
QSize MainWindow::getSize()
195
QString text = QInputDialog::getText(this, tr("Grabber"),
196
tr("Enter pixmap size:"),
198
tr("%1 x %2").arg(glWidget->width())
199
.arg(glWidget->height()),
204
QRegExp regExp(tr("([0-9]+) *x *([0-9]+)"));
205
if (regExp.exactMatch(text)) {
206
int width = regExp.cap(0).toInt();
207
int height = regExp.cap(1).toInt();
208
if (width > 0 && width < 2048 && height > 0 && height < 2048)
209
return QSize(width, height);
212
return glWidget->size();
214
<p /><address><hr /><div align="center">
215
<table width="100%" cellspacing="0" border="0"><tr class="address">
216
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
217
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
218
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
219
</tr></table></div></address></body>