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