source: trunk/examples/script/calculator/main.cpp@ 1077

Last change on this file since 1077 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 3.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QApplication>
42#include <QUiLoader>
43#include <QtScript>
44#include <QWidget>
45#include <QFile>
46#include <QMainWindow>
47#include <QLineEdit>
48
49#ifndef QT_NO_SCRIPTTOOLS
50#include <QScriptEngineDebugger>
51#endif
52
53int main(int argc, char **argv)
54{
55 Q_INIT_RESOURCE(calculator);
56
57 QApplication app(argc, argv);
58//! [0a]
59 QScriptEngine engine;
60//! [0a]
61
62#if !defined(QT_NO_SCRIPTTOOLS)
63 QScriptEngineDebugger debugger;
64 debugger.attachTo(&engine);
65 QMainWindow *debugWindow = debugger.standardWindow();
66 debugWindow->resize(1024, 640);
67#endif
68
69//! [0b]
70 QString scriptFileName(":/calculator.js");
71 QFile scriptFile(scriptFileName);
72 scriptFile.open(QIODevice::ReadOnly);
73 engine.evaluate(scriptFile.readAll(), scriptFileName);
74 scriptFile.close();
75//! [0b]
76
77//! [1]
78 QUiLoader loader;
79 QFile uiFile(":/calculator.ui");
80 uiFile.open(QIODevice::ReadOnly);
81 QWidget *ui = loader.load(&uiFile);
82 uiFile.close();
83//! [1]
84
85//! [2]
86 QScriptValue ctor = engine.evaluate("Calculator");
87 QScriptValue scriptUi = engine.newQObject(ui, QScriptEngine::ScriptOwnership);
88 QScriptValue calc = ctor.construct(QScriptValueList() << scriptUi);
89//! [2]
90
91#if !defined(QT_NO_SCRIPTTOOLS)
92 QLineEdit *display = qFindChild<QLineEdit*>(ui, "display");
93 QObject::connect(display, SIGNAL(returnPressed()),
94 debugWindow, SLOT(show()));
95#endif
96//! [3]
97 ui->show();
98 return app.exec();
99//! [3]
100}
Note: See TracBrowser for help on using the repository browser.