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