##// END OF EJS Templates
started to update/improve docs...
florianlink -
r32:ae159b178ea0
parent child
Show More
@@ -1,462 +1,507
1 1 #ifndef _PYTHONQTDOC_H
2 2 #define _PYTHONQTDOC_H
3 3
4 4 /*
5 5 *
6 6 * Copyright (C) 2006 MeVis Research GmbH 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 Research GmbH, 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 49 \mainpage PythonQt Overview
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 Qt (http://www.qtsoftware.com).
55 55 It offers an easy way to embed the Python scripting language into
56 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
59 Python wrapper around the complete Qt functionality. So if you are looking for a way to
60 write complete applications in Python using the Qt GUI, you should use PyQt.
58 The focus of PythonQt is on embedding Python into an existing C++ application, not on writing the whole
59 application completely in Python. If you want to write your whole application in Python,
60 you should use <a href="http://www.riverbankcomputing.co.uk/pyqt/">PyQt</a> instead.
61 61
62 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 66 Image Processing and Visualization platform MeVisLab (http://www.mevislab.de)
67 67 scriptable from Python.
68 68
69 \section Licensing
70
71 PythonQt is distributed under the LGPL license.
72
73 69 \section Download
74 70
75 71 PythonQt is hosted on SourceForge at http://sourceforge.net/projects/pythonqt , you can access it via SVN
76 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 96 \section Features
79 97
80 - Easy wrapping of Python objects from C++ with smart, reference-counting PythonQtObjectPtr.
81 - Convenient conversions to/from QVariant for PythonQtObjectPtr.
98 The following are the built-in features of the PythonQt library:
99
82 100 - Access all \b slots, \b properties, children and registered enums of any QObject derived class from Python
83 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 104 - Wrapping of C++ objects (which are not derived from QObject) via PythonQtCppWrapperFactory
85 105 - Extending C++ and QObject derived classes with additional slots, static methods and constructors (see Decorators)
86 106 - StdOut/Err redirection to Qt signals instead of cout
87 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 108 - Mapping of plain-old-datatypes and ALL QVariant types to and from Python
89 109 - Support for wrapping of user QVariant types which are registerd via QMetaType
90 110 - Support for Qt namespace (with all enumerators)
91 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 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 150 \section Interface
103 151
104 152 The main interface to PythonQt is the PythonQt singleton.
105 153 PythonQt needs to be initialized via PythonQt::init() once.
106 154 Afterwards you communicate with the singleton via PythonQt::self().
107 PythonQt offers a default binding for the complete QWidget set, which
108 needs to be enabled via PythonQtGui::init().
155 PythonQt offers a complete Qt binding, which
156 needs to be enabled via PythonQt_QtAll::init().
109 157
110 158
111 159 \section Datatype Datatype Mapping
112 160
113 161 The following table shows the mapping between Python and Qt objects:
114 162 <table>
115 163 <tr><th>Qt/C++</th><th>Python</th></tr>
116 164 <tr><td>bool</td><td>bool</td></tr>
117 165 <tr><td>double</td><td>float</td></tr>
118 166 <tr><td>float</td><td>float</td></tr>
119 167 <tr><td>char/uchar,int/uint,short,ushort,QChar</td><td>integer</td></tr>
120 168 <tr><td>long</td><td>integer</td></tr>
121 169 <tr><td>ulong,longlong,ulonglong</td><td>long</td></tr>
122 170 <tr><td>QString</td><td>unicode string</td></tr>
123 171 <tr><td>QByteArray</td><td>str</td></tr>
124 172 <tr><td>char*</td><td>str</td></tr>
125 173 <tr><td>QStringList</td><td>tuple of unicode strings</td></tr>
126 174 <tr><td>QVariantList</td><td>tuple of objects</td></tr>
127 175 <tr><td>QVariantMap</td><td>dict of objects</td></tr>
128 176 <tr><td>QVariant</td><td>depends on type, see below</td></tr>
129 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 180 <tr><td>EnumType</td><td>integer (all enums that are known via the moc and the Qt namespace are supported)</td></tr>
132 181 <tr><td>QObject (and derived classes)</td><td>QObject wrapper</td></tr>
133 182 <tr><td>C++ object</td><td>CPP wrapper, either wrapped via PythonQtCppWrapperFactory or just decorated with decorators</td></tr>
134 183 <tr><td>PyObject</td><td>PyObject</td></tr>
135 184 </table>
136 185
137 186 PyObject is passed as simple pointer, which allows to pass/return any Python Object directly to/from
138 187 a Qt slot.
139 188 QVariants are mapped recursively as given above, e.g. a dictionary can
140 189 contain lists of dictionaries of doubles.
141 190 For example a QVariant of type "String" is mapped to a python unicode string.
142 191 All Qt QVariant types are implemented, PythonQt supports the complete Qt API for these object.
143 192
144 193 \section QObject QObject Wrapping
145 194
146 195 All classes derived from QObject are automatically wrapped with a python wrapper class
147 196 when they become visible to the Python interpreter. This can happen via
148 197 - the PythonQt::addObject() method
149 198 - when a Qt \b slot returns a QObject derived object to python
150 199 - when a Qt \b signal contains a QObject and is connected to a python function
151 200
152 201 It is important that you call PythonQt::registerClass() for any QObject derived class
153 202 that may become visible to Python, except when you add it via PythonQt::addObject().
154 203 This will register the complete parent hierachy of the registered class, so that
155 204 when you register e.g. a QPushButton, QWidget will be registered as well (and all intermediate
156 205 parents).
157 206
158 207 From Python, you can talk to the returned QObjects in a natural way by calling
159 208 their slots and receiving the return values. You can also read/write all
160 209 properties of the objects as if they where normal python properties.
161 210
162 211 In addition to this, the wrapped objects support
163 212 - className() - returns a string that reprents the classname of the QObject
164 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 215 - connect(signal, function) - connect the signal of the given object to a python function
166 216 - connect(signal, qobject, slot) - connect the signal of the given object to a slot of another QObject
167 217 - disconnect(signal, function) - disconnect the signal of the given object from a python function
168 218 - disconnect(signal, qobject, slot) - disconnect the signal of the given object from a slot of another QObject
169 219 - children() - returns the children of the object
170 220 - setParent(QObject) - set the parent
171 221 - QObject* parent() - get the parent
172 222
173 223 The below example shows how to connect signals in Python:
174 224
175 225 \code
176 226 # define a signal handler function
177 227 def someFunction(flag):
178 228 print flag
179 229
180 230 # button1 is a QPushButton that has been added to Python via addObject()
181 231 # connect the clicked signal to a python function:
182 232 button1.connect("clicked(bool)", someFunction)
183 233
184 234 \endcode
185 235
186 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
189 and adding your factory via addWrapperFactory().
238 You can create dedicated wrapper QObjects for any C++ class. This is done by deriving from PythonQtCppWrapperFactory
239 and adding your factory via addWrapperFactory().
190 240 Whenever PythonQt encounters a CPP pointer (e.g. on a slot or signal)
191 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 242 can be passed through Python. If the wrapper factory supports the CPP class, a QObject wrapper will be created for each
193 243 instance that enters Python. An alternative to a complete wrapper via the wrapper factory are decorators, see \ref Decorators
194 244
195 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
198 inside of the "PythonQt" python module.
247 For each known C++ class, PythonQt provides a Python class. These classes are visible
248 inside of the "PythonQt" python module or in subpackages if a package is given when the class is registered.
199 249
200 250 A Meta class supports:
201 251
202 252 - access to all declared enum values
203 253 - constructors
204 - static decorator slots
254 - static methods
255 - unbound non-static methods
205 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 260 \code
210 from PythonQt import *
261 from PythonQt import QtCore
211 262
212 263 # namespace access:
213 print Qt.AlignLeft
264 print QtCore.Qt.AlignLeft
214 265
215 266 # constructors
216 a = QSize(12,13)
217 b = QFont()
267 a = QtCore.QSize(12,13)
268 b = QtCore.QFont()
218 269
219 270 # static method
220 QDate.currentDate()
271 QtCore.QDate.currentDate()
221 272
222 273 # enum value
223 QFont.UltraCondensed
274 QtCore.QFont.UltraCondensed
224 275
225 276 \endcode
226 277
227 278 \section Decorators Decorator slots
228 279
229 280 PythonQt introduces a new generic approach to extend any wrapped QObject or CPP object with
230 281
231 282 - constructors
232 283 - destructors (for CPP objects)
233 284 - additional slots
234 285 - static slots (callable on both the Meta object and the instances)
235 286
236 287 The idea behind decorators is that we wanted to make it as easy as possible to extend
237 288 wrapped objects. Since we already have an implementation for invoking any Qt Slot from
238 289 Python, it looked promising to use this approach for the extension of wrapped objects as well.
239 290 This avoids that the PythonQt user needs to care about how Python arguments are mapped from/to
240 291 Qt when he wants to create static methods, constructors and additional member functions.
241 292
242 293 The basic idea about decorators is to create a QObject derived class that implements slots
243 294 which take one of the above roles (e.g. constructor, destructor etc.) via a naming convention.
244 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 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 298 - void delete_SomeClassName(SomeClassName* o) - defines a destructor, which should delete the passed in object o
249 299 - anything static_SomeClassName_someMethodName(...) - defines a static method that is callable on instances and the meta class
250 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 302 The below example shows all kinds of decorators in action:
253 303
254 304 \code
255 305
256 306 // an example CPP object
257 307 class YourCPPObject {
258 308 public:
259 309 YourCPPObject(int arg1, float arg2) { a = arg1; b = arg2; }
260 310
261 311 float doSomething(int arg1) { return arg1*a*b; };
262 312
263 313 private:
264 314
265 315 int a;
266 316 float b;
267 317 };
268 318
269 319 // an example decorator
270 320 class ExampleDecorator : public QObject
271 321 {
272 322 Q_OBJECT
273 323
274 324 public slots:
275 // add a constructor to QSize variant that takes a QPoint
276 QVariant new_QSize(const QPoint& p) { return QSize(p.x(), p.y()); }
325 // add a constructor to QSize that takes a QPoint
326 QSize* new_QSize(const QPoint& p) { return new QSize(p.x(), p.y()); }
277 327
278 328 // add a constructor for QPushButton that takes a text and a parent widget
279 329 QPushButton* new_QPushButton(const QString& text, QWidget* parent=NULL) { return new QPushButton(text, parent); }
280 330
281 331 // add a constructor for a CPP object
282 332 YourCPPObject* new_YourCPPObject(int arg1, float arg2) { return new YourCPPObject(arg1, arg2); }
283 333
284 334 // add a destructor for a CPP object
285 335 void delete_YourCPPObject(YourCPPObject* obj) { delete obj; }
286 336
287 337 // add a static method to QWidget
288 338 QWidget* static_QWidget_mouseGrabber() { return QWidget::mouseGrabber(); }
289 339
290 340 // add an additional slot to QWidget (make move() callable, which is not declared as a slot in QWidget)
291 341 void move(QWidget* w, const QPoint& p) { w->move(p); }
292 342
293 343 // add an additional slot to QWidget, overloading the above move method
294 344 void move(QWidget* w, int x, int y) { w->move(x,y); }
295 345
296 346 // add a method to your own CPP object
297 347 int doSomething(YourCPPObject* obj, int arg1) { return obj->doSomething(arg1); }
298 348 };
299 349
300 350 ...
301 351
302 352 PythonQt::self()->addDecorators(new ExampleDecorator());
303 PythonQt::self()->registerClass(&QPushButton::staticMetaObject);
304 PythonQt::self()->registerCPPClassNames(QStringList() << "YourCPPObject");
353 PythonQt::self()->registerCPPClass("YourCPPObject");
305 354
306 355 \endcode
307 356
308 357 After you have registered an instance of the above ExampleDecorator, you can do the following from Python
309 358 (all these calls are mapped to the above decorator slots):
310 359
311 360 \code
312 from PythonQt import *
361 from PythonQt import QtCore, QtGui, YourCPPObject
313 362
314 363 # call our new constructor of QSize
315 size = QSize(QPoint(1,2));
364 size = QtCore.QSize(QPoint(1,2));
316 365
317 366 # call our new QPushButton constructor
318 button = QPushButton("sometext");
367 button = QtGui.QPushButton("sometext");
319 368
320 369 # call the move slot (overload1)
321 370 button.move(QPoint(0,0))
322 371
323 372 # call the move slot (overload2)
324 373 button.move(0,0)
325 374
326 375 # call the static method
327 grabber = QWidget.mouseWrapper();
376 grabber = QtGui.QWidget.mouseWrapper();
328 377
329 378 # create a CPP object via constructor
330 379 yourCpp = YourCPPObject(1,11.5)
331 380
332 381 # call the wrapped method on CPP object
333 382 print yourCpp.doSomething(1);
334 383
335 384 # destructor will be called:
336 385 yourCpp = None
337 386
338 387 \endcode
339 388
340 389 \section Building
341 390
342 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 392 To compile PythonQt, you will need a python developer installation which includes Python's header files and
344 393 the python2x.[lib | dll | so | dynlib].
345 394 The build scripts a currently set to use Python 2.5.
346 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 397 \subsection Windows
349 398
350 399 On Windows, the (non-source) Python Windows installer can be used.
351 400 Make sure that you use the same compiler, the current Python distribution is built
352 401 with Visual Studio 2003. If you want to use another compiler, you will need to build
353 402 Python yourself, using your compiler.
354 403
355 404 To build PythonQt, you need to set the environment variable \b PYTHON_PATH to point to the root
356 405 dir of the python installation and \b PYTHON_LIB to point to
357 406 the directory where the python lib file is located.
358 407
359 408 When using the prebuild Python installer, this will be:
360 409
361 410 \code
362 411 > set PYTHON_PATH = c:\Python25
363 412 > set PYTHON_LIB = c:\Python25\libs
364 413 \endcode
365 414
366 415 When using the python sources, this will be something like:
367 416
368 417 \code
369 418 > set PYTHON_PATH = c:\yourDir\Python-2.5.1\
370 419 > set PYTHON_LIB = c:\yourDir\Python-2.5.1\PCbuild8\Win32
371 420 \endcode
372 421
373 422 To build all, do the following (after setting the above variables):
374 423
375 424 \code
376 425 > cd PythonQtRoot
377 426 > vcvars32
378 427 > qmake
379 428 > nmake
380 429 \endcode
381 430
382 431 This should build everything. If Python can not be linked or include files can not be found,
383 432 you probably need to tweak \b build/python.prf
384 433
385 434 The tests and examples are located in PythonQt/lib.
386 435
387 436 \subsection Linux
388 437
389 438 On Linux, you need to install a Python-dev package.
390 439 If Python can not be linked or include files can not be found,
391 440 you probably need to tweak \b build/python.prf
392 441
393 442 To build PythonQt, just do a:
394 443
395 444 \code
396 445 > cd PythonQtRoot
397 446 > qmake
398 447 > make all
399 448 \endcode
400 449
401 450 The tests and examples are located in PythonQt/lib.
402 451 You should add PythonQt/lib to your LD_LIBRARY_PATH so that the runtime
403 452 linker can find the *.so files.
404 453
405 454 \subsection MacOsX
406 455
407 456 On Mac, Python is installed as a Framework, so you should not need to install it.
408 457 To build PythonQt, just do a:
409 458
410 459 \code
411 460 > cd PythonQtRoot
412 461 > qmake
413 462 > make all
414 463 \endcode
415 464
416 465 \section Tests
417 466
418 467 There is a unit test that tests most features of PythonQt, see the \b tests subdirectory for details.
419 468
420 469 \section Examples
421 470
422 471 Examples are available in the \b examples directory. The PyScriptingConsole implements a simple
423 472 interactive scripting console that shows how to script a simple application.
424 473
425 474 The following shows how to integrate PythonQt into you Qt application:
426 475
427 476 \code
428 477 #include "PythonQt.h"
429 478 #include <QApplication>
430 479 ...
431 480
432 481 int main( int argc, char **argv )
433 482 {
434 483
435 484 QApplication qapp(argc, argv);
436 485
437 486 // init PythonQt and Python itself
438 487 PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
439 488
489
440 490 // get a smart pointer to the __main__ module of the Python interpreter
441 491 PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
442 492
443 493 // add a QObject as variable of name "example" to the namespace of the __main__ module
444 494 PyExampleObject example;
445 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 497 // do something
453 498 PythonQt::self()->runScript(mainContext, "print example\n");
454 499 PythonQt::self()->runScript(mainContext, "def multiply(a,b):\n return a*b;\n");
455 500 QVariantList args;
456 501 args << 42 << 47;
457 502 QVariant result = PythonQt::self()->call(mainContext,"multiply", args);
458 503 ...
459 504 \endcode
460 505
461 506
462 507 */
General Comments 0
You need to be logged in to leave comments. Login now