~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/corelib/doc/snippets/code/src_corelib_kernel_qmetaobject.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the documentation of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:BSD$
 
9
** You may use this file under the terms of the BSD license as follows:
 
10
**
 
11
** "Redistribution and use in source and binary forms, with or without
 
12
** modification, are permitted provided that the following conditions are
 
13
** met:
 
14
**   * Redistributions of source code must retain the above copyright
 
15
**     notice, this list of conditions and the following disclaimer.
 
16
**   * Redistributions in binary form must reproduce the above copyright
 
17
**     notice, this list of conditions and the following disclaimer in
 
18
**     the documentation and/or other materials provided with the
 
19
**     distribution.
 
20
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
 
21
**     of its contributors may be used to endorse or promote products derived
 
22
**     from this software without specific prior written permission.
 
23
**
 
24
**
 
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 
36
**
 
37
** $QT_END_LICENSE$
 
38
**
 
39
****************************************************************************/
 
40
 
 
41
 
 
42
void wrapInFunction()
 
43
{
 
44
 
 
45
//! [0]
 
46
class MyClass : public QObject
 
47
{
 
48
    Q_OBJECT
 
49
    Q_CLASSINFO("author", "Sabrina Schweinsteiger")
 
50
    Q_CLASSINFO("url", "http://doc.moosesoft.co.uk/1.0/")
 
51
 
 
52
public:
 
53
    ...
 
54
};
 
55
//! [0]
 
56
 
 
57
 
 
58
//! [1]
 
59
QByteArray normType = QMetaObject::normalizedType(" int    const  *");
 
60
// normType is now "const int*"
 
61
//! [1]
 
62
 
 
63
 
 
64
//! [2]
 
65
QMetaObject::invokeMethod(thread, "quit",
 
66
                          Qt::QueuedConnection);
 
67
//! [2]
 
68
 
 
69
 
 
70
//! [3]
 
71
QMetaObject::invokeMethod: Unable to handle unregistered datatype 'MyType'
 
72
//! [3]
 
73
 
 
74
 
 
75
//! [4]
 
76
QString retVal;
 
77
QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection,
 
78
                          Q_RETURN_ARG(QString, retVal),
 
79
                          Q_ARG(QString, "sqrt"),
 
80
                          Q_ARG(int, 42),
 
81
                          Q_ARG(double, 9.7));
 
82
//! [4]
 
83
 
 
84
 
 
85
//! [5]
 
86
class MyClass
 
87
{
 
88
    Q_OBJECT
 
89
    Q_CLASSINFO("author", "Sabrina Schweinsteiger")
 
90
    Q_CLASSINFO("url", "http://doc.moosesoft.co.uk/1.0/")
 
91
 
 
92
public:
 
93
    ...
 
94
};
 
95
//! [5]
 
96
 
 
97
 
 
98
//! [propertyCount]
 
99
const QMetaObject* metaObject = obj->metaObject();
 
100
QStringList properties;
 
101
for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i)
 
102
    properties << QString::fromLatin1(metaObject->property(i).name());
 
103
//! [propertyCount]
 
104
 
 
105
 
 
106
//! [methodCount]
 
107
const QMetaObject* metaObject = obj->metaObject();
 
108
QStringList methods;
 
109
for(int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i)
 
110
    methods << QString::fromLatin1(metaObject->method(i).methodSignature());
 
111
//! [methodCount]
 
112
 
 
113
//! [6]
 
114
int methodIndex = pushButton->metaObject()->indexOfMethod("animateClick()");
 
115
QMetaMethod method = metaObject->method(methodIndex);
 
116
method.invoke(pushButton, Qt::QueuedConnection);
 
117
//! [6]
 
118
 
 
119
//! [7]
 
120
QMetaMethod::invoke: Unable to handle unregistered datatype 'MyType'
 
121
//! [7]
 
122
 
 
123
//! [8]
 
124
QString retVal;
 
125
QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QString, int, double)");
 
126
int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature);
 
127
QMetaMethod method = metaObject->method(methodIndex);
 
128
method.invoke(obj,
 
129
              Qt::DirectConnection,
 
130
              Q_RETURN_ARG(QString, retVal),
 
131
              Q_ARG(QString, "sqrt"),
 
132
              Q_ARG(int, 42),
 
133
              Q_ARG(double, 9.7));
 
134
//! [8]
 
135
 
 
136
//! [9]
 
137
QMetaMethod destroyedSignal = QMetaMethod::fromSignal(&QObject::destroyed);
 
138
//! [9]
 
139
 
 
140
}