From 7d27356bafd5670adcc8753ab5437b3bf8ffa4be Mon Sep 17 00:00:00 2001
From: Timothy Pearson
In this last example we will wrap a fictional C++ library that contains a class -that is derived from a Qt class. This will demonstrate how SIP allows a class +that is derived from a TQt class. This will demonstrate how SIP allows a class hierarchy to be split across multiple Python extension modules, and will introduce SIP’s versioning system.
The library contains a single C++ class called Hello which is derived from -Qt’s QLabel class. It behaves just like QLabel except that the text +TQt’s TQLabel class. It behaves just like TQLabel except that the text in the label is hard coded to be Hello World. To make the example more -interesting we’ll also say that the library only supports Qt v4.2 and later, +interesting we’ll also say that the library only supports TQt v4.2 and later, and also includes a function called setDefault() that is not implemented in the Windows version of the library.
The hello.h header file looks something like this:
@@ -251,12 +251,13 @@ in the Windows version of the library. #include <qwidget.h> #include <qstring.h> -class Hello : public QLabel { - // This is needed by the Qt Meta-Object Compiler. +class Hello : public TQLabel { + // This is needed by the TQt Meta-Object Compiler. Q_OBJECT + TQ_OBJECT public: - Hello(QWidget *parent = 0); + Hello(TQWidget *parent = 0); private: // Prevent instances from being copied. @@ -265,7 +266,7 @@ private: }; #if !defined(Q_OS_WIN) -void setDefault(const QString &def); +void setDefault(const TQString &def); #endifThe corresponding SIP specification file would then look something like this:
@@ -273,25 +274,25 @@ void setDefault(const QString &def); %Module hello 0 -%Import QtGui/QtGuimod.sip +%Import TQtGui/TQtGuimod.sip -%If (Qt_4_2_0 -) +%If (TQt_4_2_0 -) -class Hello : QLabel { +class Hello : TQLabel { %TypeHeaderCode #include <hello.h> %End public: - Hello(QWidget *parent /TransferThis/ = 0); + Hello(TQWidget *parent /TransferThis/ = 0); private: Hello(const Hello &); }; %If (!WS_WIN) -void setDefault(const QString &def); +void setDefault(const TQString &def); %End %End @@ -301,12 +302,12 @@ previous examples.
- The %Import directive has been added to specify that we are -extending the class hierarchy defined in the file QtGui/QtGuimod.sip. -This file is part of PyQt. The build system will take care of finding +extending the class hierarchy defined in the file TQtGui/TQtGuimod.sip. +This file is part of PyTQt. The build system will take care of finding the file’s exact location.
- The %If directive has been added to specify that everything -[4] up to the matching %End directive only applies to Qt -v4.2 and later. Qt_4_2_0 is a tag defined in QtCoremod.sip +[4] up to the matching %End directive only applies to TQt +v4.2 and later. TQt_4_2_0 is a tag defined in TQtCoremod.sip [5] using the %Timeline directive. %Timeline is used to define a tag for each version of a library’s API you are wrapping allowing you to maintain all the different versions in a single @@ -318,9 +319,9 @@ removed. This is not supported by SIP.
- The TransferThis annotation has been added to the constructor’s argument. It specifies that if the argument is not 0 (i.e. the Hello instance being constructed has a parent) then ownership of the instance -is transferred from Python to C++. It is needed because Qt maintains -objects (i.e. instances derived from the QObject class) in a -hierachy. When an object is destroyed all of its children are also +is transferred from Python to C++. It is needed because TQt maintains +objects (i.e. instances derived from the TQObject class) in a +hierachy. When an object is destroyed all of its tqchildren are also automatically destroyed. It is important, therefore, that the Python garbage collector doesn’t also try and destroy them. This is covered in more detail in Ownership of Objects. SIP provides many other @@ -331,7 +332,7 @@ values.
SIP.- The %If directive has been added to specify that everything up to the matching %End directive does not apply to Windows. -WS_WIN is another tag defined by PyQt, this time using the +WS_WIN is another tag defined by PyTQt, this time using the %Platforms directive. Tags defined by the %Platforms directive are mutually exclusive, i.e. only one may be valid at a time [6].
@@ -342,9 +343,9 @@ copy constructor when it can never be called from Python? The answer is to prevent the automatic generation of a public copy constructor.We now look at the configure.py script. This is a little different to the script in the previous examples for two related reasons.
-Firstly, PyQt includes a pure Python module called pyqtconfig that extends -the SIP build system for modules, like our example, that build on top of PyQt. -It deals with the details of which version of Qt is being used (i.e. it +
Firstly, PyTQt includes a pure Python module called pyqtconfig that extends +the SIP build system for modules, like our example, that build on top of PyTQt. +It deals with the details of which version of TQt is being used (i.e. it determines what the correct tags are) and where it is installed. This is called a module’s configuration module.
Secondly, we generate a configuration module (called helloconfig) for our @@ -354,16 +355,16 @@ life easier for them.
Now we have two scripts. First the configure.py script:
import os import sipconfig -from PyQt4 import pyqtconfig +from PyTQt4 import pyqtconfig # The name of the SIP build file generated by SIP and used by the build # system. build_file = "hello.sbf" -# Get the PyQt configuration information. +# Get the PyTQt configuration information. config = pyqtconfig.Configuration() -# Get the extra SIP flags needed by the imported PyQt modules. Note that +# Get the extra SIP flags needed by the imported PyTQt modules. Note that # this normally only includes those flags (-x and -t) that relate to SIP's # versioning system. pyqt_sip_flags = config.pyqt_sip_flags @@ -380,10 +381,10 @@ life easier for them. installs.append(["helloconfig.py", config.default_mod_dir]) -# Create the Makefile. The QtGuiModuleMakefile class provided by the +# Create the Makefile. The TQtGuiModuleMakefile class provided by the # pyqtconfig module takes care of all the extra preprocessor, compiler and -# linker flags needed by the Qt library. -makefile = pyqtconfig.QtGuiModuleMakefile( +# linker flags needed by the TQt library. +makefile = pyqtconfig.TQtGuiModuleMakefile( configuration=config, build_file=build_file, installs=installs @@ -418,7 +419,7 @@ life easier for them.Next we have the helloconfig.py.in template script:
-from PyQt4 import pyqtconfig +from PyTQt4 import pyqtconfig # These are installation specific values created when Hello was configured. # The following line will be replaced when this template is used to create @@ -445,7 +446,7 @@ life easier for them. pyqtconfig.Configuration.__init__(self, cfg) -class HelloModuleMakefile(pyqtconfig.QtGuiModuleMakefile): +class HelloModuleMakefile(pyqtconfig.TQtGuiModuleMakefile): """The Makefile class for modules that %Import hello. """ def finalise(self): @@ -455,7 +456,7 @@ life easier for them. self.extra_libs.append("hello") # Let the super-class do what it needs to. - pyqtconfig.QtGuiModuleMakefile.finalise(self) + pyqtconfig.TQtGuiModuleMakefile.finalise(self)Again, we hope that the scripts are self documenting.
@@ -468,8 +469,8 @@ life easier for them.@@ -535,13 +536,13 @@ by importing modules. sub-classed from one of the SIP provided types. Your types must be registered using sipRegisterPyType(). This is normally done in code specified using the %InitialisationCode directive. -
- [5] Actually in versions.sip. PyQt uses the %Include -directive to split the SIP specification for Qt across a large number of + [5] Actually in versions.sip. PyTQt uses the %Include +directive to split the SIP specification for TQt across a large number of separate .sip files. As an example, PyQt4 uses %DefaultMetatype to specify a new -meta-type that handles the interaction with Qt’s own meta-type system. It also +
As an example, PyTQt4 uses %DefaultMetatype to specify a new +meta-type that handles the interaction with TQt’s own meta-type system. It also uses %DefaultSupertype to specify that the smaller sip.simplewrapper super-type is normally used. Finally it uses -Supertype as an annotation of the QObject class to override the +Supertype as an annotation of the TQObject class to override the default and use sip.wrapper as the super-type so that the parent/child -relationships of QObject instances are properly maintained.
+relationships of TQObject instances are properly maintained.Lazy Type Attributes¶
@@ -566,8 +567,8 @@ ignored.Support for Wide Characters¶
SIP v4.6 introduced support for wide characters (i.e. the wchar_t type). -Python’s C API includes support for converting between unicode objects and wide -character strings and arrays. When converting from a unicode object to wide +Python’s C API includes support for converting between tqunicode objects and wide +character strings and arrays. When converting from a tqunicode object to wide characters SIP creates the string or array on the heap (using memory allocated using sipMalloc()). This then raises the problem of how this memory is subsequently freed.
-- cgit v1.2.1