##// END OF EJS Templates
started to update/improve docs...
florianlink -
r32:ae159b178ea0
parent child
Show More
@@ -1,462 +1,507
1 #ifndef _PYTHONQTDOC_H
1 #ifndef _PYTHONQTDOC_H
2 #define _PYTHONQTDOC_H
2 #define _PYTHONQTDOC_H
3
3
4 /*
4 /*
5 *
5 *
6 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
6 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
7 *
7 *
8 * This library is free software; you can redistribute it and/or
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
11 * version 2.1 of the License, or (at your option) any later version.
12 *
12 *
13 * This library is distributed in the hope that it will be useful,
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
16 * Lesser General Public License for more details.
17 *
17 *
18 * Further, this software is distributed without any warranty that it is
18 * Further, this software is distributed without any warranty that it is
19 * free of the rightful claim of any third person regarding infringement
19 * free of the rightful claim of any third person regarding infringement
20 * or the like. Any license provided herein, whether implied or
20 * or the like. Any license provided herein, whether implied or
21 * otherwise, applies only to this software file. Patent licenses, if
21 * otherwise, applies only to this software file. Patent licenses, if
22 * any, provided herein do not apply to combinations of this program with
22 * any, provided herein do not apply to combinations of this program with
23 * other software, or any other product whatsoever.
23 * other software, or any other product whatsoever.
24 *
24 *
25 * You should have received a copy of the GNU Lesser General Public
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
28 *
29 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
29 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
30 * 28359 Bremen, Germany or:
30 * 28359 Bremen, Germany or:
31 *
31 *
32 * http://www.mevis.de
32 * http://www.mevis.de
33 *
33 *
34 */
34 */
35
35
36 //----------------------------------------------------------------------------------
36 //----------------------------------------------------------------------------------
37 /*!
37 /*!
38 // \file PythonQtDoc.h
38 // \file PythonQtDoc.h
39 // \author Florian Link
39 // \author Florian Link
40 // \author Last changed by $Author: florian $
40 // \author Last changed by $Author: florian $
41 // \date 2006-10
41 // \date 2006-10
42 */
42 */
43 //----------------------------------------------------------------------------------
43 //----------------------------------------------------------------------------------
44
44
45 /*!
45 /*!
46 \if USE_GLOBAL_DOXYGEN_DOC
46 \if USE_GLOBAL_DOXYGEN_DOC
47 \page PythonQtPage PythonQt Overview
47 \page PythonQtPage PythonQt Overview
48 \else
48 \else
49 \mainpage PythonQt Overview
49 \mainpage PythonQt Overview
50 \endif
50 \endif
51
51
52 \section Introduction
52 \section Introduction
53
53
54 \b PythonQt is a dynamic Python (http://www.python.org) binding for Qt (http://www.qtsoftware.com).
54 \b PythonQt is a dynamic Python (http://www.python.org) binding for Qt (http://www.qtsoftware.com).
55 It offers an easy way to embed the Python scripting language into
55 It offers an easy way to embed the Python scripting language into
56 your Qt applications. It makes heavy use of the QMetaObject system and thus requires Qt4.x.
56 your Qt applications. It makes heavy use of the QMetaObject system and thus requires Qt4.x.
57
57
58 In contrast to <a href="http://www.riverbankcomputing.co.uk/pyqt/">PyQt</a> , PythonQt is \b not a complete
58 The focus of PythonQt is on embedding Python into an existing C++ application, not on writing the whole
59 Python wrapper around the complete Qt functionality. So if you are looking for a way to
59 application completely in Python. If you want to write your whole application in Python,
60 write complete applications in Python using the Qt GUI, you should use PyQt.
60 you should use <a href="http://www.riverbankcomputing.co.uk/pyqt/">PyQt</a> instead.
61
61
62 If you are looking for a simple way to embed Python objects into your C++/Qt Application
62 If you are looking for a simple way to embed Python objects into your C++/Qt Application
63 and to script parts of your application via Python, PythonQt is the way to go!
63 and to script parts of your application via Python, PythonQt is the way to go!
64
64
65 PythonQt is a stable library that was developed to make the
65 PythonQt is a stable library that was developed to make the
66 Image Processing and Visualization platform MeVisLab (http://www.mevislab.de)
66 Image Processing and Visualization platform MeVisLab (http://www.mevislab.de)
67 scriptable from Python.
67 scriptable from Python.
68
68
69 \section Licensing
70
71 PythonQt is distributed under the LGPL license.
72
73 \section Download
69 \section Download
74
70
75 PythonQt is hosted on SourceForge at http://sourceforge.net/projects/pythonqt , you can access it via SVN
71 PythonQt is hosted on SourceForge at http://sourceforge.net/projects/pythonqt , you can access it via SVN
76 or download a tarball.
72 or download a tarball.
77
73
74 \section Licensing
75
76 PythonQt is distributed under the LGPL license, so it pairs well with the LGPL of the Qt 4.5 release and allows
77 to be used in commercial applications when following the LGPL 2.1 obligations.
78
79 \section LicensingWrapper Licensing of Wrapper Generator
80
81 The build system of PythonQt makes use of a modified version of the GPL'ed QtScript generator,
82 located in the "generator" directory.
83
84 See http://labs.trolltech.com/page/Projects/QtScript/Generator for details on the original project.
85 Thanks a lot to the QtJambi guys and the QtScript Generator project for the C++ parser and
86 Qt typesystem files!
87
88 The PythonQt wrappers generated by the generator located in the "generated_cpp" directory are distributed under the LGPL,
89 they are not restriced by the GPL.
90
91 The generated wrappers are pre-generated and checked-in for Qt 4.4.3, so you only need to build and run the
92 generator when you want to build additional wrappers or you want to upgrade/downgrade to an newer Qt version.
93 You may use the generator to generate C++ bindings for your own C++ classes (e.g. to make them deriveable in Python),
94 , but this is currently not documented and involves creating your own typesystem files.
95
78 \section Features
96 \section Features
79
97
80 - Easy wrapping of Python objects from C++ with smart, reference-counting PythonQtObjectPtr.
98 The following are the built-in features of the PythonQt library:
81 - Convenient conversions to/from QVariant for PythonQtObjectPtr.
99
82 - Access all \b slots, \b properties, children and registered enums of any QObject derived class from Python
100 - Access all \b slots, \b properties, children and registered enums of any QObject derived class from Python
83 - Connecting Qt Signals to Python functions (both from within Python and from C++)
101 - Connecting Qt Signals to Python functions (both from within Python and from C++)
102 - Easy wrapping of Python objects from C++ with smart, reference-counting PythonQtObjectPtr.
103 - Convenient conversions to/from QVariant for PythonQtObjectPtr.
84 - Wrapping of C++ objects (which are not derived from QObject) via PythonQtCppWrapperFactory
104 - Wrapping of C++ objects (which are not derived from QObject) via PythonQtCppWrapperFactory
85 - Extending C++ and QObject derived classes with additional slots, static methods and constructors (see Decorators)
105 - Extending C++ and QObject derived classes with additional slots, static methods and constructors (see Decorators)
86 - StdOut/Err redirection to Qt signals instead of cout
106 - StdOut/Err redirection to Qt signals instead of cout
87 - Interface for creating your own \c import replacement, so that Python scripts can be e.g. signed/verified before they are executed (PythonQtImportFileInterface)
107 - Interface for creating your own \c import replacement, so that Python scripts can be e.g. signed/verified before they are executed (PythonQtImportFileInterface)
88 - Mapping of plain-old-datatypes and ALL QVariant types to and from Python
108 - Mapping of plain-old-datatypes and ALL QVariant types to and from Python
89 - Support for wrapping of user QVariant types which are registerd via QMetaType
109 - Support for wrapping of user QVariant types which are registerd via QMetaType
90 - Support for Qt namespace (with all enumerators)
110 - Support for Qt namespace (with all enumerators)
91 - All PythonQt wrapped objects support the dir() statement, so that you can see easily which attributes a QObject, CPP object or QVariant has
111 - All PythonQt wrapped objects support the dir() statement, so that you can see easily which attributes a QObject, CPP object or QVariant has
92 - No preprocessing/wrapping tool needs to be started, PythonQt can script any QObject without prior knowledge about it (except for the MetaObject information from the \b moc)
112 - No preprocessing/wrapping tool needs to be started, PythonQt can script any QObject without prior knowledge about it (except for the MetaObject information from the \b moc)
113 - Multiple inheritance for C++ objects (e.g. a QWidget is derived from QObject and QPaintDevice, PythonQt will automatically cast a QWidget to a QPaintDevice when needed)
114 - Polymorphic downcasting (if e.g. PythonQt sees a QEvent, it can downcast it depending on the type(), so the Python e.g. sees a QPaintEvent instead of a plain QEvent)
115 - Deriving C++ objects from Python and overwriting virtual method with a Python implementation (requires usage of wrapper generator or manual work!)
116 - Extensible handler for Python/C++ conversion of complex types, e.g. mapping of QVector<SomeObject> to/from a Python array
117
118 \section FeaturesQtAll Features (with PythonQt_QtAll linked in)
119
120 Thanks to the new wrapper generator, PythonQt now offers the additional PythonQt_QtAll library which wraps the complete Qt API, including all C++ classes and all non-slots on QObject derived classes.
121 This offers the following features:
122
123 - Complete Qt API wrapped and accessible
124 - The following modules are available as submodule of the PythonQt module:
125 - QtCore
126 - QtGui
127 - QtNetwork
128 - QtOpenGL
129 - QtSql
130 - QtSvg
131 - QtUiTools
132 - QtWebKit
133 - QtXml
134 - QtXmlPatterns
135 - (phonon, QtHelp, assistant, designer are currently not supported, this would require some additional effort on the code generator)
136 - For convenience, all classes are also available in the PythonQt.Qt module, for people who do not care in which module a class is located
137 - Any Qt class that has virtual methods can be easily derived from Python and the virtual methods can be reimplemented in Python
138 - Polymorphic downcasting on QEvent, QGraphicsItem, QStyleOption, ...
139 - Multiple inheritance support (e.g. QGraphicsTextItem is a QObject AND a QGraphicsItem, PythonQt will handle this well)
140
141 \section Comparision Comparision with PyQt
142
143 - PythonQt is not as Pythonic as PyQt in many details (e.g. operator mapping, pickling, translation support, ...) and it is maily thought for embedding and intercommunication between Qt/Cpp and Python
144 - PythonQt allows to communicate in both directions, e.g. calling a Python object from C++ AND calling a C++ method from Python, while PyQt only handles the Python->C++ direction
145 - PythonQt offers properties as Python attributes, while PyQt offers them as setter/getter methods (e.g. QWidget.width is a property in PythonQt and a method in PyQt)
146 - PythonQt does not auto-convert objects, e.g. when a QPainter expects a QBrush and you pass a QColor, it is rejected, you will need to write QBrush(QColor(1,2,3)) instead
147 - Probably there are lots of details that differ, I do not know PyQt that well to list them all.
93
148
94 \section Non-Features
95
96 Features that PythonQt does NOT support (and will not support):
97
98 - you can not derive from QObjects inside of Python, this would require wrapper generation like PyQt does
99 - you can only script QObject derived classes, for normal C++ classes you need to create a PythonQtCppWrapperFactory and adequate wrapper classes or add decorator slots
100 - you can not access normal member functions of QObjects, only slots and properties, because the \b moc does not store normal member functions in the MetaObject system
101
149
102 \section Interface
150 \section Interface
103
151
104 The main interface to PythonQt is the PythonQt singleton.
152 The main interface to PythonQt is the PythonQt singleton.
105 PythonQt needs to be initialized via PythonQt::init() once.
153 PythonQt needs to be initialized via PythonQt::init() once.
106 Afterwards you communicate with the singleton via PythonQt::self().
154 Afterwards you communicate with the singleton via PythonQt::self().
107 PythonQt offers a default binding for the complete QWidget set, which
155 PythonQt offers a complete Qt binding, which
108 needs to be enabled via PythonQtGui::init().
156 needs to be enabled via PythonQt_QtAll::init().
109
157
110
158
111 \section Datatype Datatype Mapping
159 \section Datatype Datatype Mapping
112
160
113 The following table shows the mapping between Python and Qt objects:
161 The following table shows the mapping between Python and Qt objects:
114 <table>
162 <table>
115 <tr><th>Qt/C++</th><th>Python</th></tr>
163 <tr><th>Qt/C++</th><th>Python</th></tr>
116 <tr><td>bool</td><td>bool</td></tr>
164 <tr><td>bool</td><td>bool</td></tr>
117 <tr><td>double</td><td>float</td></tr>
165 <tr><td>double</td><td>float</td></tr>
118 <tr><td>float</td><td>float</td></tr>
166 <tr><td>float</td><td>float</td></tr>
119 <tr><td>char/uchar,int/uint,short,ushort,QChar</td><td>integer</td></tr>
167 <tr><td>char/uchar,int/uint,short,ushort,QChar</td><td>integer</td></tr>
120 <tr><td>long</td><td>integer</td></tr>
168 <tr><td>long</td><td>integer</td></tr>
121 <tr><td>ulong,longlong,ulonglong</td><td>long</td></tr>
169 <tr><td>ulong,longlong,ulonglong</td><td>long</td></tr>
122 <tr><td>QString</td><td>unicode string</td></tr>
170 <tr><td>QString</td><td>unicode string</td></tr>
123 <tr><td>QByteArray</td><td>str</td></tr>
171 <tr><td>QByteArray</td><td>str</td></tr>
124 <tr><td>char*</td><td>str</td></tr>
172 <tr><td>char*</td><td>str</td></tr>
125 <tr><td>QStringList</td><td>tuple of unicode strings</td></tr>
173 <tr><td>QStringList</td><td>tuple of unicode strings</td></tr>
126 <tr><td>QVariantList</td><td>tuple of objects</td></tr>
174 <tr><td>QVariantList</td><td>tuple of objects</td></tr>
127 <tr><td>QVariantMap</td><td>dict of objects</td></tr>
175 <tr><td>QVariantMap</td><td>dict of objects</td></tr>
128 <tr><td>QVariant</td><td>depends on type, see below</td></tr>
176 <tr><td>QVariant</td><td>depends on type, see below</td></tr>
129 <tr><td>QSize, QRect and all other standard Qt QVariants</td><td>variant wrapper that supports complete API of the respective Qt classes</td></tr>
177 <tr><td>QSize, QRect and all other standard Qt QVariants</td><td>variant wrapper that supports complete API of the respective Qt classes</td></tr>
130 <tr><td>OwnRegisteredMetaType</td><td>variant wrapper, optionally with a wrapper provided by addVariantWrapper()</td></tr>
178 <tr><td>OwnRegisteredMetaType</td><td>C++ wrapper, optionally with additional information/wrapping provided by registerCPPClass()</td></tr>
179 <tr><td>QList<AnyObject*></td><td>converts to a list of CPP wrappers</td></tr>
131 <tr><td>EnumType</td><td>integer (all enums that are known via the moc and the Qt namespace are supported)</td></tr>
180 <tr><td>EnumType</td><td>integer (all enums that are known via the moc and the Qt namespace are supported)</td></tr>
132 <tr><td>QObject (and derived classes)</td><td>QObject wrapper</td></tr>
181 <tr><td>QObject (and derived classes)</td><td>QObject wrapper</td></tr>
133 <tr><td>C++ object</td><td>CPP wrapper, either wrapped via PythonQtCppWrapperFactory or just decorated with decorators</td></tr>
182 <tr><td>C++ object</td><td>CPP wrapper, either wrapped via PythonQtCppWrapperFactory or just decorated with decorators</td></tr>
134 <tr><td>PyObject</td><td>PyObject</td></tr>
183 <tr><td>PyObject</td><td>PyObject</td></tr>
135 </table>
184 </table>
136
185
137 PyObject is passed as simple pointer, which allows to pass/return any Python Object directly to/from
186 PyObject is passed as simple pointer, which allows to pass/return any Python Object directly to/from
138 a Qt slot.
187 a Qt slot.
139 QVariants are mapped recursively as given above, e.g. a dictionary can
188 QVariants are mapped recursively as given above, e.g. a dictionary can
140 contain lists of dictionaries of doubles.
189 contain lists of dictionaries of doubles.
141 For example a QVariant of type "String" is mapped to a python unicode string.
190 For example a QVariant of type "String" is mapped to a python unicode string.
142 All Qt QVariant types are implemented, PythonQt supports the complete Qt API for these object.
191 All Qt QVariant types are implemented, PythonQt supports the complete Qt API for these object.
143
192
144 \section QObject QObject Wrapping
193 \section QObject QObject Wrapping
145
194
146 All classes derived from QObject are automatically wrapped with a python wrapper class
195 All classes derived from QObject are automatically wrapped with a python wrapper class
147 when they become visible to the Python interpreter. This can happen via
196 when they become visible to the Python interpreter. This can happen via
148 - the PythonQt::addObject() method
197 - the PythonQt::addObject() method
149 - when a Qt \b slot returns a QObject derived object to python
198 - when a Qt \b slot returns a QObject derived object to python
150 - when a Qt \b signal contains a QObject and is connected to a python function
199 - when a Qt \b signal contains a QObject and is connected to a python function
151
200
152 It is important that you call PythonQt::registerClass() for any QObject derived class
201 It is important that you call PythonQt::registerClass() for any QObject derived class
153 that may become visible to Python, except when you add it via PythonQt::addObject().
202 that may become visible to Python, except when you add it via PythonQt::addObject().
154 This will register the complete parent hierachy of the registered class, so that
203 This will register the complete parent hierachy of the registered class, so that
155 when you register e.g. a QPushButton, QWidget will be registered as well (and all intermediate
204 when you register e.g. a QPushButton, QWidget will be registered as well (and all intermediate
156 parents).
205 parents).
157
206
158 From Python, you can talk to the returned QObjects in a natural way by calling
207 From Python, you can talk to the returned QObjects in a natural way by calling
159 their slots and receiving the return values. You can also read/write all
208 their slots and receiving the return values. You can also read/write all
160 properties of the objects as if they where normal python properties.
209 properties of the objects as if they where normal python properties.
161
210
162 In addition to this, the wrapped objects support
211 In addition to this, the wrapped objects support
163 - className() - returns a string that reprents the classname of the QObject
212 - className() - returns a string that reprents the classname of the QObject
164 - help() - shows all properties, slots, enums, decorator slots and constructors of the object, in a printable form
213 - help() - shows all properties, slots, enums, decorator slots and constructors of the object, in a printable form
214 - delete() - deletes the object (use with care, especially if you passed the ownership to C++)
165 - connect(signal, function) - connect the signal of the given object to a python function
215 - connect(signal, function) - connect the signal of the given object to a python function
166 - connect(signal, qobject, slot) - connect the signal of the given object to a slot of another QObject
216 - connect(signal, qobject, slot) - connect the signal of the given object to a slot of another QObject
167 - disconnect(signal, function) - disconnect the signal of the given object from a python function
217 - disconnect(signal, function) - disconnect the signal of the given object from a python function
168 - disconnect(signal, qobject, slot) - disconnect the signal of the given object from a slot of another QObject
218 - disconnect(signal, qobject, slot) - disconnect the signal of the given object from a slot of another QObject
169 - children() - returns the children of the object
219 - children() - returns the children of the object
170 - setParent(QObject) - set the parent
220 - setParent(QObject) - set the parent
171 - QObject* parent() - get the parent
221 - QObject* parent() - get the parent
172
222
173 The below example shows how to connect signals in Python:
223 The below example shows how to connect signals in Python:
174
224
175 \code
225 \code
176 # define a signal handler function
226 # define a signal handler function
177 def someFunction(flag):
227 def someFunction(flag):
178 print flag
228 print flag
179
229
180 # button1 is a QPushButton that has been added to Python via addObject()
230 # button1 is a QPushButton that has been added to Python via addObject()
181 # connect the clicked signal to a python function:
231 # connect the clicked signal to a python function:
182 button1.connect("clicked(bool)", someFunction)
232 button1.connect("clicked(bool)", someFunction)
183
233
184 \endcode
234 \endcode
185
235
186 \section CPP CPP Wrapping
236 \section CPP CPP Wrapping
187
237
188 You can create dedicated wrapper QObject for any C++ class. This is done by deriving from PythonQtCppWrapperFactory
238 You can create dedicated wrapper QObjects for any C++ class. This is done by deriving from PythonQtCppWrapperFactory
189 and adding your factory via addWrapperFactory().
239 and adding your factory via addWrapperFactory().
190 Whenever PythonQt encounters a CPP pointer (e.g. on a slot or signal)
240 Whenever PythonQt encounters a CPP pointer (e.g. on a slot or signal)
191 and it does not known it as a QObject derived class, it will create a generic CPP wrapper. So even unknown C++ objects
241 and it does not known it as a QObject derived class, it will create a generic CPP wrapper. So even unknown C++ objects
192 can be passed through Python. If the wrapper factory supports the CPP class, a QObject wrapper will be created for each
242 can be passed through Python. If the wrapper factory supports the CPP class, a QObject wrapper will be created for each
193 instance that enters Python. An alternative to a complete wrapper via the wrapper factory are decorators, see \ref Decorators
243 instance that enters Python. An alternative to a complete wrapper via the wrapper factory are decorators, see \ref Decorators
194
244
195 \section MetaObject Meta Object/Class access
245 \section MetaObject Meta Object/Class access
196
246
197 For each known CPP class, QObject derived class and QVariant type, PythonQt provides a Meta class. These meta classes are visible
247 For each known C++ class, PythonQt provides a Python class. These classes are visible
198 inside of the "PythonQt" python module.
248 inside of the "PythonQt" python module or in subpackages if a package is given when the class is registered.
199
249
200 A Meta class supports:
250 A Meta class supports:
201
251
202 - access to all declared enum values
252 - access to all declared enum values
203 - constructors
253 - constructors
204 - static decorator slots
254 - static methods
255 - unbound non-static methods
205 - help() and className()
256 - help() and className()
206
257
207 From within Python, you can import the module "PythonQt" to access these meta objects and the Qt namespace.
258 From within Python, you can import the module "PythonQt" to access these classes and the Qt namespace.
208
259
209 \code
260 \code
210 from PythonQt import *
261 from PythonQt import QtCore
211
262
212 # namespace access:
263 # namespace access:
213 print Qt.AlignLeft
264 print QtCore.Qt.AlignLeft
214
265
215 # constructors
266 # constructors
216 a = QSize(12,13)
267 a = QtCore.QSize(12,13)
217 b = QFont()
268 b = QtCore.QFont()
218
269
219 # static method
270 # static method
220 QDate.currentDate()
271 QtCore.QDate.currentDate()
221
272
222 # enum value
273 # enum value
223 QFont.UltraCondensed
274 QtCore.QFont.UltraCondensed
224
275
225 \endcode
276 \endcode
226
277
227 \section Decorators Decorator slots
278 \section Decorators Decorator slots
228
279
229 PythonQt introduces a new generic approach to extend any wrapped QObject or CPP object with
280 PythonQt introduces a new generic approach to extend any wrapped QObject or CPP object with
230
281
231 - constructors
282 - constructors
232 - destructors (for CPP objects)
283 - destructors (for CPP objects)
233 - additional slots
284 - additional slots
234 - static slots (callable on both the Meta object and the instances)
285 - static slots (callable on both the Meta object and the instances)
235
286
236 The idea behind decorators is that we wanted to make it as easy as possible to extend
287 The idea behind decorators is that we wanted to make it as easy as possible to extend
237 wrapped objects. Since we already have an implementation for invoking any Qt Slot from
288 wrapped objects. Since we already have an implementation for invoking any Qt Slot from
238 Python, it looked promising to use this approach for the extension of wrapped objects as well.
289 Python, it looked promising to use this approach for the extension of wrapped objects as well.
239 This avoids that the PythonQt user needs to care about how Python arguments are mapped from/to
290 This avoids that the PythonQt user needs to care about how Python arguments are mapped from/to
240 Qt when he wants to create static methods, constructors and additional member functions.
291 Qt when he wants to create static methods, constructors and additional member functions.
241
292
242 The basic idea about decorators is to create a QObject derived class that implements slots
293 The basic idea about decorators is to create a QObject derived class that implements slots
243 which take one of the above roles (e.g. constructor, destructor etc.) via a naming convention.
294 which take one of the above roles (e.g. constructor, destructor etc.) via a naming convention.
244 These slots are then assigned to other classes via the naming convention.
295 These slots are then assigned to other classes via the naming convention.
245
296
246 - QVariant new_SomeClassName(...) - defines a constructor for "SomeClassName" that returns a QVariant
247 - SomeClassName* new_SomeClassName(...) - defines a constructor for "SomeClassName" that returns a new object of type SomeClassName (where SomeClassName can be any CPP class, not just QObject classes)
297 - SomeClassName* new_SomeClassName(...) - defines a constructor for "SomeClassName" that returns a new object of type SomeClassName (where SomeClassName can be any CPP class, not just QObject classes)
248 - void delete_SomeClassName(SomeClassName* o) - defines a destructor, which should delete the passed in object o
298 - void delete_SomeClassName(SomeClassName* o) - defines a destructor, which should delete the passed in object o
249 - anything static_SomeClassName_someMethodName(...) - defines a static method that is callable on instances and the meta class
299 - anything static_SomeClassName_someMethodName(...) - defines a static method that is callable on instances and the meta class
250 - anything someMethodName(SomeClassName* o, ...) - defines a slot that will be available on SomeClassName instances (and derived instances). When such a slot is called the first argument is the pointer to the instance and the rest of the arguments can be used to make a call on the instance.
300 - anything someMethodName(SomeClassName* o, ...) - defines a slot that will be available on SomeClassName instances (and derived instances). When such a slot is called the first argument is the pointer to the instance and the rest of the arguments can be used to make a call on the instance.
251
301
252 The below example shows all kinds of decorators in action:
302 The below example shows all kinds of decorators in action:
253
303
254 \code
304 \code
255
305
256 // an example CPP object
306 // an example CPP object
257 class YourCPPObject {
307 class YourCPPObject {
258 public:
308 public:
259 YourCPPObject(int arg1, float arg2) { a = arg1; b = arg2; }
309 YourCPPObject(int arg1, float arg2) { a = arg1; b = arg2; }
260
310
261 float doSomething(int arg1) { return arg1*a*b; };
311 float doSomething(int arg1) { return arg1*a*b; };
262
312
263 private:
313 private:
264
314
265 int a;
315 int a;
266 float b;
316 float b;
267 };
317 };
268
318
269 // an example decorator
319 // an example decorator
270 class ExampleDecorator : public QObject
320 class ExampleDecorator : public QObject
271 {
321 {
272 Q_OBJECT
322 Q_OBJECT
273
323
274 public slots:
324 public slots:
275 // add a constructor to QSize variant that takes a QPoint
325 // add a constructor to QSize that takes a QPoint
276 QVariant new_QSize(const QPoint& p) { return QSize(p.x(), p.y()); }
326 QSize* new_QSize(const QPoint& p) { return new QSize(p.x(), p.y()); }
277
327
278 // add a constructor for QPushButton that takes a text and a parent widget
328 // add a constructor for QPushButton that takes a text and a parent widget
279 QPushButton* new_QPushButton(const QString& text, QWidget* parent=NULL) { return new QPushButton(text, parent); }
329 QPushButton* new_QPushButton(const QString& text, QWidget* parent=NULL) { return new QPushButton(text, parent); }
280
330
281 // add a constructor for a CPP object
331 // add a constructor for a CPP object
282 YourCPPObject* new_YourCPPObject(int arg1, float arg2) { return new YourCPPObject(arg1, arg2); }
332 YourCPPObject* new_YourCPPObject(int arg1, float arg2) { return new YourCPPObject(arg1, arg2); }
283
333
284 // add a destructor for a CPP object
334 // add a destructor for a CPP object
285 void delete_YourCPPObject(YourCPPObject* obj) { delete obj; }
335 void delete_YourCPPObject(YourCPPObject* obj) { delete obj; }
286
336
287 // add a static method to QWidget
337 // add a static method to QWidget
288 QWidget* static_QWidget_mouseGrabber() { return QWidget::mouseGrabber(); }
338 QWidget* static_QWidget_mouseGrabber() { return QWidget::mouseGrabber(); }
289
339
290 // add an additional slot to QWidget (make move() callable, which is not declared as a slot in QWidget)
340 // add an additional slot to QWidget (make move() callable, which is not declared as a slot in QWidget)
291 void move(QWidget* w, const QPoint& p) { w->move(p); }
341 void move(QWidget* w, const QPoint& p) { w->move(p); }
292
342
293 // add an additional slot to QWidget, overloading the above move method
343 // add an additional slot to QWidget, overloading the above move method
294 void move(QWidget* w, int x, int y) { w->move(x,y); }
344 void move(QWidget* w, int x, int y) { w->move(x,y); }
295
345
296 // add a method to your own CPP object
346 // add a method to your own CPP object
297 int doSomething(YourCPPObject* obj, int arg1) { return obj->doSomething(arg1); }
347 int doSomething(YourCPPObject* obj, int arg1) { return obj->doSomething(arg1); }
298 };
348 };
299
349
300 ...
350 ...
301
351
302 PythonQt::self()->addDecorators(new ExampleDecorator());
352 PythonQt::self()->addDecorators(new ExampleDecorator());
303 PythonQt::self()->registerClass(&QPushButton::staticMetaObject);
353 PythonQt::self()->registerCPPClass("YourCPPObject");
304 PythonQt::self()->registerCPPClassNames(QStringList() << "YourCPPObject");
305
354
306 \endcode
355 \endcode
307
356
308 After you have registered an instance of the above ExampleDecorator, you can do the following from Python
357 After you have registered an instance of the above ExampleDecorator, you can do the following from Python
309 (all these calls are mapped to the above decorator slots):
358 (all these calls are mapped to the above decorator slots):
310
359
311 \code
360 \code
312 from PythonQt import *
361 from PythonQt import QtCore, QtGui, YourCPPObject
313
362
314 # call our new constructor of QSize
363 # call our new constructor of QSize
315 size = QSize(QPoint(1,2));
364 size = QtCore.QSize(QPoint(1,2));
316
365
317 # call our new QPushButton constructor
366 # call our new QPushButton constructor
318 button = QPushButton("sometext");
367 button = QtGui.QPushButton("sometext");
319
368
320 # call the move slot (overload1)
369 # call the move slot (overload1)
321 button.move(QPoint(0,0))
370 button.move(QPoint(0,0))
322
371
323 # call the move slot (overload2)
372 # call the move slot (overload2)
324 button.move(0,0)
373 button.move(0,0)
325
374
326 # call the static method
375 # call the static method
327 grabber = QWidget.mouseWrapper();
376 grabber = QtGui.QWidget.mouseWrapper();
328
377
329 # create a CPP object via constructor
378 # create a CPP object via constructor
330 yourCpp = YourCPPObject(1,11.5)
379 yourCpp = YourCPPObject(1,11.5)
331
380
332 # call the wrapped method on CPP object
381 # call the wrapped method on CPP object
333 print yourCpp.doSomething(1);
382 print yourCpp.doSomething(1);
334
383
335 # destructor will be called:
384 # destructor will be called:
336 yourCpp = None
385 yourCpp = None
337
386
338 \endcode
387 \endcode
339
388
340 \section Building
389 \section Building
341
390
342 PythonQt requires at least Qt 4.2.2 (or higher) and Python 2.3, 2.4, 2.5 or 2.6 on Windows, Linux and MacOS X. It has not yet been tested with Python 3.x, but it should only require minor changes.
391 PythonQt requires at least Qt 4.2.2 (or higher) and Python 2.3, 2.4, 2.5 or 2.6 on Windows, Linux and MacOS X. It has not yet been tested with Python 3.x, but it should only require minor changes.
343 To compile PythonQt, you will need a python developer installation which includes Python's header files and
392 To compile PythonQt, you will need a python developer installation which includes Python's header files and
344 the python2x.[lib | dll | so | dynlib].
393 the python2x.[lib | dll | so | dynlib].
345 The build scripts a currently set to use Python 2.5.
394 The build scripts a currently set to use Python 2.5.
346 You may need to tweak the \b build/python.prf file to set the correct Python includes and libs on your system.
395 You may need to tweak the \b build/python.prf file to set the correct Python includes and libs on your system.
347
396
348 \subsection Windows
397 \subsection Windows
349
398
350 On Windows, the (non-source) Python Windows installer can be used.
399 On Windows, the (non-source) Python Windows installer can be used.
351 Make sure that you use the same compiler, the current Python distribution is built
400 Make sure that you use the same compiler, the current Python distribution is built
352 with Visual Studio 2003. If you want to use another compiler, you will need to build
401 with Visual Studio 2003. If you want to use another compiler, you will need to build
353 Python yourself, using your compiler.
402 Python yourself, using your compiler.
354
403
355 To build PythonQt, you need to set the environment variable \b PYTHON_PATH to point to the root
404 To build PythonQt, you need to set the environment variable \b PYTHON_PATH to point to the root
356 dir of the python installation and \b PYTHON_LIB to point to
405 dir of the python installation and \b PYTHON_LIB to point to
357 the directory where the python lib file is located.
406 the directory where the python lib file is located.
358
407
359 When using the prebuild Python installer, this will be:
408 When using the prebuild Python installer, this will be:
360
409
361 \code
410 \code
362 > set PYTHON_PATH = c:\Python25
411 > set PYTHON_PATH = c:\Python25
363 > set PYTHON_LIB = c:\Python25\libs
412 > set PYTHON_LIB = c:\Python25\libs
364 \endcode
413 \endcode
365
414
366 When using the python sources, this will be something like:
415 When using the python sources, this will be something like:
367
416
368 \code
417 \code
369 > set PYTHON_PATH = c:\yourDir\Python-2.5.1\
418 > set PYTHON_PATH = c:\yourDir\Python-2.5.1\
370 > set PYTHON_LIB = c:\yourDir\Python-2.5.1\PCbuild8\Win32
419 > set PYTHON_LIB = c:\yourDir\Python-2.5.1\PCbuild8\Win32
371 \endcode
420 \endcode
372
421
373 To build all, do the following (after setting the above variables):
422 To build all, do the following (after setting the above variables):
374
423
375 \code
424 \code
376 > cd PythonQtRoot
425 > cd PythonQtRoot
377 > vcvars32
426 > vcvars32
378 > qmake
427 > qmake
379 > nmake
428 > nmake
380 \endcode
429 \endcode
381
430
382 This should build everything. If Python can not be linked or include files can not be found,
431 This should build everything. If Python can not be linked or include files can not be found,
383 you probably need to tweak \b build/python.prf
432 you probably need to tweak \b build/python.prf
384
433
385 The tests and examples are located in PythonQt/lib.
434 The tests and examples are located in PythonQt/lib.
386
435
387 \subsection Linux
436 \subsection Linux
388
437
389 On Linux, you need to install a Python-dev package.
438 On Linux, you need to install a Python-dev package.
390 If Python can not be linked or include files can not be found,
439 If Python can not be linked or include files can not be found,
391 you probably need to tweak \b build/python.prf
440 you probably need to tweak \b build/python.prf
392
441
393 To build PythonQt, just do a:
442 To build PythonQt, just do a:
394
443
395 \code
444 \code
396 > cd PythonQtRoot
445 > cd PythonQtRoot
397 > qmake
446 > qmake
398 > make all
447 > make all
399 \endcode
448 \endcode
400
449
401 The tests and examples are located in PythonQt/lib.
450 The tests and examples are located in PythonQt/lib.
402 You should add PythonQt/lib to your LD_LIBRARY_PATH so that the runtime
451 You should add PythonQt/lib to your LD_LIBRARY_PATH so that the runtime
403 linker can find the *.so files.
452 linker can find the *.so files.
404
453
405 \subsection MacOsX
454 \subsection MacOsX
406
455
407 On Mac, Python is installed as a Framework, so you should not need to install it.
456 On Mac, Python is installed as a Framework, so you should not need to install it.
408 To build PythonQt, just do a:
457 To build PythonQt, just do a:
409
458
410 \code
459 \code
411 > cd PythonQtRoot
460 > cd PythonQtRoot
412 > qmake
461 > qmake
413 > make all
462 > make all
414 \endcode
463 \endcode
415
464
416 \section Tests
465 \section Tests
417
466
418 There is a unit test that tests most features of PythonQt, see the \b tests subdirectory for details.
467 There is a unit test that tests most features of PythonQt, see the \b tests subdirectory for details.
419
468
420 \section Examples
469 \section Examples
421
470
422 Examples are available in the \b examples directory. The PyScriptingConsole implements a simple
471 Examples are available in the \b examples directory. The PyScriptingConsole implements a simple
423 interactive scripting console that shows how to script a simple application.
472 interactive scripting console that shows how to script a simple application.
424
473
425 The following shows how to integrate PythonQt into you Qt application:
474 The following shows how to integrate PythonQt into you Qt application:
426
475
427 \code
476 \code
428 #include "PythonQt.h"
477 #include "PythonQt.h"
429 #include <QApplication>
478 #include <QApplication>
430 ...
479 ...
431
480
432 int main( int argc, char **argv )
481 int main( int argc, char **argv )
433 {
482 {
434
483
435 QApplication qapp(argc, argv);
484 QApplication qapp(argc, argv);
436
485
437 // init PythonQt and Python itself
486 // init PythonQt and Python itself
438 PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
487 PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
439
488
489
440 // get a smart pointer to the __main__ module of the Python interpreter
490 // get a smart pointer to the __main__ module of the Python interpreter
441 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
491 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
442
492
443 // add a QObject as variable of name "example" to the namespace of the __main__ module
493 // add a QObject as variable of name "example" to the namespace of the __main__ module
444 PyExampleObject example;
494 PyExampleObject example;
445 PythonQt::self()->addObject(mainContext, "example", &example);
495 PythonQt::self()->addObject(mainContext, "example", &example);
446
496
447 // register all other QObjects that you want to script and that are returned by your API
448 PythonQt::self()->registerClass(&QMainWindow::staticMetaObject);
449 PythonQt::self()->registerClass(&QPushButton::staticMetaObject);
450 ...
451
452 // do something
497 // do something
453 PythonQt::self()->runScript(mainContext, "print example\n");
498 PythonQt::self()->runScript(mainContext, "print example\n");
454 PythonQt::self()->runScript(mainContext, "def multiply(a,b):\n return a*b;\n");
499 PythonQt::self()->runScript(mainContext, "def multiply(a,b):\n return a*b;\n");
455 QVariantList args;
500 QVariantList args;
456 args << 42 << 47;
501 args << 42 << 47;
457 QVariant result = PythonQt::self()->call(mainContext,"multiply", args);
502 QVariant result = PythonQt::self()->call(mainContext,"multiply", args);
458 ...
503 ...
459 \endcode
504 \endcode
460
505
461
506
462 */
507 */
General Comments 0
You need to be logged in to leave comments. Login now