Sign in Join
Wiki copytemporary

Qt4 binding: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11*(Qt only)*
22
33# Download Free Pascal Qt4 Binding
44
55The binding releases are uploaded to the [FPC Qt4 Binding Home Page](http://users.telenet.be/Jan.Van.hijfte/qtforfpc/fpcqt4.html).
66
77# Install Free Pascal Qt4 Binding
88
99The download contains a file *README.TXT* which contains the installation instructions.
1010
1111# Links
1212
1313- [Qt Documentation](http://doc.trolltech.com/)
1414- [Pascal Qt4 binding home page](http://users.telenet.be/Jan.Van.hijfte/qtforfpc/fpcqt4.html)
1515- [Pascal Qt2/E binding home page](http://users.telenet.be/Jan.Van.hijfte/qtforfpc/qtedemo.html)
1616- [[Qt Interface|Lazarus Qt4 interface]]
1717- [[Qt Interface Mac|Compile instructions for Mac]]
1818- [[Qt4 Maemo Binding|FreePascal Qt4 Binding for Maemo]]
1919
2020# How to use the Qt4 binding
2121
2222## License
2323
2424Since Qt4 is also available under LGPL, there is no difference in licensing with e.g. Gtk. LGPL allows development of both free and proprietary software without any license fees or royalties. [Nokia Qt License info](http://qt.nokia.com/products/licensing)
2525
2626## Demo Programs
2727
2828The FPC Qt4 Binding is not meant to create Qt applications in FPC. But it should allow the Lazarus LCL developers to create a Qt Widget set for Lazarus. A Lazarus Qt Widget set allows Lazarus developers to create programs for the entire set of platforms supported by Qt (Windows, Linux, Linux Embedded, OS X, ...). The provided demoes on [FPC Qt4 Binding Home Page](http://users.telenet.be/Jan.Van.hijfte/qtforfpc/fpcqt4.html) provide insight in how to use the binding for LCL/Qt development. The lazarus user may however also choose to make some direct Qt4 binding calls to achieve things not yet supported in Lazarus/LCL such as WebKit support.
2929
3030The Qt2 Embedded binding was intended to create full FPC Qt programs and therefore included an extra advanced object oriented layer of Pascal classes and interfaces on top of the Qt classes. [Qt/E demo](http://users.pandora.be/Jan.Van.hijfte/qtforfpc/qtedemo.html). Though this extra layer is convenient, it was chosen not to create one for Qt4 to avoid any performance penalty or code bloat for LCL/Qt.
3131
3232## Use the Qt Documentation
3333
3434### How to create a Class
3535
3636Look up the Qt Class you would like to use. Suppose it is [QWidget](http://doc.trolltech.com/4.6/qwidget.html). Some classes provide several constructors (with different parameters). The binding provides for most constructors a corresponding factory function QtClassName_create(parameters). Store the class in a variable of type QtClassName+H. The beginning of Qt4.pas provides you with a list of all supported classes. That same list also serves to provide type safe usage of the Qt class handles. The list encodes the Class hierarchy.
3737
3838``` pascal
3939 var W : QWidgetH;
4040 ...
4141 W := QWidget_create(nil,0);
4242
4343```
4444
4545### How to call a method of a Qt Class
4646
4747Use the Nokia Qt documentaton to find out how and when to use which method. Suppose you want to use [void QWidget::setFixedHeight ( int h )](http://doc.trolltech.com/4.5/qwidget.html#setFixedHeight) The corresponding Pascal fuction will be ClassName+_+MethodName. So look in Qt4.pas for the definition of QWidget_setFixedHeight
4848
4949``` pascal
5050 procedure QWidget_setFixedHeight(handle: QWidgetH; h: Integer);
5151
5252```
5353
5454Note how the pascal function has an additional parameter: the Qt Class instance handle of your QWidget Class. Calling the setFixedHeight method will be like this
5555
5656``` pascal
5757 QWidget_ setFixedHeight(W,100);
5858
5959```
6060
6161### How to hook a Pascal Method to a signal of a Qt Class
6262
6363Qt classes can emit signals and other classes can hook method (slots) to these signals. The binding allows to hook Pascal methods to the Qt classes. To be able to do this, an intermediate Qt class needs to be created. For every class that emits signals, that intermediate class, referred to as hook, is provided in the binding and is called ClassName_hookH. This class can connect to all signals of its corresponding class and will call the corresponding pascal method upon occurrence of the signal.
6464
6565Example: The Qt Class QAbstractButton has a [signal clicked](http://doc.trolltech.com/4.5/qabstractbutton.html#clicked). The hook class of the binding is QAbstractButton_hookH.
6666
6767``` pascal
6868 var
6969 hook : QAbstractButton_hookH;
7070 ...
7171 hook:=QAbstractButton_hook_create(AbstractButtonHandle);
7272 QAbstractButton_hook_hook_clicked(hook, @Clicked);
7373
7474```
7575
7676The definition of hook_hook_clicked shows the type of the Pascal Method to provide:
7777
7878``` pascal
7979 procedure QAbstractButton_hook_hook_clicked(handle: QAbstractButton_hookH; hook: QAbstractButton_clicked_Event);
8080
8181```
8282
8383That Pascal Method type is defined as:
8484
8585``` pascal
8686 type
8787 QAbstractButton_clicked_Event = procedure (checked: Boolean = False) of object '''cdecl''';
8888
8989```
9090
9191Therefore the method Clicked should be defined as: (**Take note of the cdecl calling convention**)
9292
9393``` pascal
9494 TButton = class (TObject)
9595 ...
9696 procedure Clicked(checked: Boolean);'''cdecl''';
9797 ...
9898 procedure TButton.Clicked(checked: Boolean = False);'''cdecl''';
9999 begin
100100 end;
101101
102102```
103103
104104### C++ Multiple inheritance
105105
106106Some Qt Classes inherit from more than 1 class. QWidget inherits from QObject and QPaintDevice. When you create in C++ a QWidget instance, there are actually two instances that get created. One instance handle for QObject and one for QPaintDevice. In the binding, the returned handle will be for the first parent (QObject). To use the methods provided through inheritance by the second parent, you need to use the second instance handle. That second instance handle is obtained by using a function called FirstClass_To_SecondClass:
107107
108108``` pascal
109109 function QWidget_to_QPaintDevice(handle: QWidgetH): QPaintDeviceH;
110110
111111```
112112
113113In the demo program you notice how the paint event is used to paint on a QWidget. To paint, you need to create a QPainter (like canvas) from the QWidget. The Qt documentation shows that the constructor of QPainter requires a QPaintDevice. So here is an extract of the demo
114114
115115``` pascal
116116 var PaintBox : QWidgetH;
117117 PaintBox:=QWidget_create();
118118 ...
119119 // In the paint method
120120 var CV : QPainterH;
121121 CV:=QPainter_create(QWidget_to_QPaintDevice(PaintBox));
122122
123123```
124124
125125Do not use typecasting like QPaintDeviceH(PaintBox). The compiler would complain if you did this anyhow. See type safety below.
126126
127127# Technical information
128128
129129## Supported Platforms
130130
131131The Qt library works on different platforms such as Linux/BSD, Windows, OS X and Embedded. Depending of the platform a number of methods are available or not.
132132
133133Example functions:
134134
135135- Windows only function: QPixmap_toWinHBITMAP
136136- Linux only function: QApplication_x11EventFilter
137137- macOS only function: QApplication_macEventFilter
138138- Embedded only function: QApplication_qwsEventFilter
139139
140140Qt achieves this with conditional defines such as Q_WS_MAC or Q_WS_QWS. There are many more Q_XXX that allow to fine tune the Qt library for many platforms. You can e.g. configure Qt to not include CUPS support with QT_NO_CUPS. The Qt library source that is contained in the Windows Qt Download or the Embedded download is pretty similar, only very platform specific include files are omitted. The key difference is that the config files are different, e.g. in qconfig.h Q_WS_QWS is enabled in the Embedded Qt Download and not in the qconfig.h from other Platform downloads
141141
142142The pascal Qt interface library also uses a single source for all platforms by using conditional defines. Not as many defines as in Qt are used, the sole aim was to cope with the different platforms: Linux, Windows, OS X and Embedded.
143143
144144This is achieved by defining for a given platform a number of QT_XXX defines and then converting the headers with moc. Moc allows the provision of defines so that the methods that are available when these defines are set, are the only ones visible.
145145
146146The different binding platform defines are:
147147
148148| Platform define | List of Qt defines |
149149|-----------------|----------------------------------------------------|
150150| BINUX | Q_WS_X11 Q_OS_LINUX Q_OS_UNIX |
151151| MSWINDOWS | Q_WS_WIN |
152152| DARWIN | Q_WS_MAC Q_OS_UNIX |
153153| QTOPIA | Q_WS_QWS Q_OS_LINUX Q_OS_UNIX QT_NO_SESSIONMANAGER |
154154
155155Platforms
156156
157157Binux stands for BSD/Linux.
158158
159159The binding consists of a C++ part, that uses above defines to enclose platform specific methods. To compile the code on a given platform, the respective define should be set. In the V1.X versions, the platform specific compile scripts defined the appropriate platform, in the V2.X the provided qmake project automatically sets the proper platform define.
160160
161161The binding also consists of a pascal part. In that part the different defines are enabled by using the free pascal predefined defines or they just are the same.
162162
163163Note that embedded is a special platform, that is both linux and embedded. (I did not look into the recently created wince and symbian qt variant yet). But to avoid the inclusion of any missing X11 libraries, you should not define BINUX for the compilation of the library or qt4.pas. The V1.X library source package for Linux contains 2 compile scripts, one for linux (compile_lib.sh) that defines BINUX and one for Qt/Embedded (compile_lib_qtopia.sh) that defines QTOPIA and allows calling either gcc (intel qvfb) or arm-linux-gcc (arm embedded). In the V2.X bindings, you need to set QTOPIA yourself in the qmake project.
164164
165165The lazarus lcl qt interface, also uses platform specific defines in the lcl code that calls the Qt binding. To correctly compile the lcl for embedded, you need to set the define QTOPIA. The other used defines (LINUX, WINDOWS, DARWIN=macOS) are done automatically by using existing platform specific defines provided by fpc.
166166
167167To compile lazarus for embedded/qtopia:
168168
169-` make PREFIX=/usr clean all LCL_PLATFORM=qt OPT="-dQTOPIA"`
169+```text
170+ make PREFIX=/usr clean all LCL_PLATFORM=qt OPT="-dQTOPIA"
171+```
170172
171173<http://users.telenet.be/Jan.Van.hijfte/qtforfpc/Lazarus_Embedded.png> last Version verified to work is 0.9.29
172174
173175## Type safe Qt Class Handles
174176
175177In Qt4.pas you will see a large list of type definitions of Qt Class handles.
176178
177179``` pascal
178180 QWidgetH = class(QObjectH) end;
179181 QAbstractButtonH = class(QWidgetH) end;
180182 QPushButtonH = class(QAbstractButtonH) end;
181183
182184```
183185
184186This list ensures type safety and encodes the class hierarchy.
185187
186188### Inheritance
187189
188190This way you can call an inherited function using a descendant class type. The extract of the list shows that QPushButton descends from QWidget, so you can call the inherited method "width" from QWidget with your QPushButton handle
189191
190192``` pascal
191193 var Btn : QPushButtonH;
192194 ...
193195 Btn := QPushButton_create(parent);
194196 writeln('Button Width:',QWidget_width(Btn));
195197
196198```
197199
198200### Type Safe Class Parameters
199201
200202In the Multiple inheritance paragraph, you have seen that you need to call a convert function to convert your QWidget to a QPaintDevice. If you didn't use the convert the compiler would complain. Suppose we omit the convert function
201203
202204``` pascal
203205
204206 // Do not do this !!!
205207 CV:=QPainter_create(PaintBox);
206208
207209```
208210
209211The type safety net will make the compiler complain like this:
210212
211-`demo.pas(265,30) Error: Incompatible type for arg no. 1: Got "QWidgetH", expected "QPaintDeviceH"`
213+``` pascal
214+demo.pas(265,30) Error: Incompatible type for arg no. 1: Got "QWidgetH", expected "QPaintDeviceH"
215+```
212216
213217Similar if you would not use the convert function, but would just use typecast like this
214218
215219``` pascal
216220 // Do '''not''' do this !!!
217221 CV:=QPainter_create(QPaintDeviceH(PaintBox));
218222
219223```
220224
221225The compiler will complain with:
222226
223-`demo.pas(266,21) Warning: Class types "QWidgetH" and "QPaintDeviceH" are not related`
227+```text
228+demo.pas(266,21) Warning: Class types "QWidgetH" and "QPaintDeviceH" are not related
229+```
224230
225231That typecast would not change the handle. Handles are just numbers, the returned handle/number of QWidget_to_QPaintDevice is different than the PaintBox handle/number. It indirectly points to a different Virtual Method Table.
226232
227233### Type Safe Method Parameters
228234
229235The Pascal methods that can be provided to the Hook classes, need to be of the correct function type. This prevents otherwise not so pleasant stack problems when the binding c++ code calls the provided pascal method.
230236
231237## Qt Enums and QFlags
232238
233239Qt uses a template class QFlags\<Enum\> when passing combinations of an Enum to a method. This Qt Doc [WindowType](http://doc.trolltech.com/4.5/qt.html#WindowType-enum) defines the enum Qt::WindowType and the associated Qt Class Qt::WindowFlags. In the header file the flags are declared like this
234240
235241``` cpp
236242 Q_DECLARE_FLAGS(WindowFlags, WindowType)
237243
238244```
239245
240246More about this macro: [Q_DECLARE_FLAGS](http://doc.trolltech.com/4.4/qflags.html#Q_DECLARE_FLAGS)
241247
242248Alot of methods have a QFlags Qt Class as a parameter. Normally we would need to create a instance of this class, pass it to the method and delete it ourselves in the pascal code. To avoid all this hassle the binding generator changed all methods to allow for passing a pascal cardinal type.
243249
244250Let's sample this with the following constructor [QMainWindow](http://doc.trolltech.com/4.0/qmainwindow.html#QMainWindow). The constructor needs the QFlags Qt::WFlags. The Qt doc shows that this is just a synonym for Qt::WindowFlags. The following Qt4.pas extract show that the enums are declared as cardinals, so that "or" combination are allowed ("or" on pascal enums is not allowed). Sometimes these enums are used as default value of a parameter in a function. As pascal does not allow typed constants in default values, the different values of the enums are declared as untyped constants. The corresponding QFlags\<\> type is simply redeclared as the same type as the enum. Its name and the comments allow for easy cross reference Qt4.pas and the Qt documentation.
245251
246252``` pascal
247253 type
248254 QtWindowType = cardinal; // Qt::WindowType (4)
249255 QtWindowFlags = QtWindowType; // QFlags<>
250256 const
251257 QtWidget = 0 { $0 };
252258 QtWindow = 1 { $1 };
253259 QtDialog = 3 { $3 };
254260 ... etc
255261
256262```
257263
258264Now lets use this on QMainWindow. The following code creates a MainWindow that stays on top and has a small toolwindow title bar:
259265
260266``` pascal
261267 MainWindow:=QMainWindow_create(nil,QtTool or QtWindowStaysOnTopHint);
262268
263269```
264270
265271## Floating Point Exceptions
266272
267273Throughout the Qt-library, Trolltech does not check for divisions by zero for floating points. No exceptions are generated because FPEs (Floating Point Exceptions) have been disabled. However when you link an FPC program to a library, FPC re-enables FPEs. To solve this the call fedisableexcept(FE_ALL_EXCEPT) was added to Qt4.pas. Otherwise several graphic painting calls would cause exceptions.
268274
269275E.g. a call to QPainter::drawLine() will ultimately lead to a call to qt_tesselate_polygon in qpaintengine_x11.cpp where a division by zero will lead to an exception in the line:
270276
271277``` cpp
272278 edge.m = (p1.y() - p2.y()) / (p1.x() - p2.x()); // line derivative
273279
274280```
275281
276282## Pascalify Qt Classes
277283
278284### What is the problem
279285
280286The binding allows you to create Qt classes by reference but not by value. Assignment of one Qt class by value to another is impossible, because the pascal compiler does not know the interior of a C++ Qt Class. As this may not be clear, let us make an example.
281287
282288### Example of the problem
283289
284290In C++ you can write:
285291
286292``` cpp
287293 QColor hourColor(127, 0, 127);
288294 painter.setBrush(hourColor)
289295
290296```
291297
292298At the end of this procedure you do not have to delete the QColor instance. The binding without pascalify of QColor would force you to write:
293299
294300``` pascal
295301 var hourColor : QColorH;//*QColor
296302 hourColor:=QColor_create(127,0,127); //new QColor()
297303 brush:=QBruh_create(hourColor);
298304 QColor_destroy(hourColor)
299305
300306```
301307
302308The destroy is rather annoying for small classes like QRect and QColor.
303309
304310### Solution:Pascalify
305311
306312Pascalify means the creation of a pascal record that is binary compatible with the Qt class. Qt classes without a VMT like QColor, QRect or QRectF are easy targets. If you provide a pointer to this record, the C++ compiler will not notice the difference. So now you can simply write
307313
308314``` pascal
309315 var HourColor : TQColor;// a pascal record type compatible with QColor
310316 QColor_fromRGB(@hourColor,127,0,127);
311317 brush:=QBrush_create(@hourColor);
312318```
313319
314320Now you do not need to free the QColor so there is less chance for a memory leak.
315321
316322Note: QRect and PRect use the same trick but with some extra adjustments for two reasons:
317323
318-`* In macOS the layout of QRect is different than the layout of TRect in pascal.`\
319-`* The TRect definition of LCL is different from Qt, with respect to the inclusion of the right and bottom line.`
324+```text
325+* In macOS the layout of QRect is different than the layout of TRect in pascal.
326+* The TRect definition of LCL is different from Qt, with respect to the inclusion of the right and bottom line.
327+```
320328
321329The interface adjust with +1 when going to Qt or with -1 when going to Pascal.
322330
323331Internal code sample for clarity:
324332
325333``` cpp
326334 inline void copyPRectToQRect(PRect pr, QRect &qr)
327335 {
328336 #if defined DARWIN
329337 qr.setLeft(((QRect *)pr)->top());
330338 qr.setTop(((QRect *)pr)->left());
331339 qr.setRight(((QRect *)pr)->bottom()-1);
332340 qr.setBottom(((QRect *)pr)->right()-1);
333341 #else
334342 qr.setLeft(((QRect *)pr)->left());
335343 qr.setTop(((QRect *)pr)->top());
336344 qr.setRight(((QRect *)pr)->right()-1);
337345 qr.setBottom(((QRect *)pr)->bottom()-1);
338346 #endif
339347 }
340348```
341349
342350# FAQ
343351
344352## I get linker error /usr/bin/ld: cannot find -lQt4Pas
345353
346354The Qt4Pas comes with a library (e.g. under Linux libqt4.so) which must be installed. See installation.
347355
348356---
349357*From the Free Pascal wiki, page "Qt4 binding", as it stood on 2020-02-23 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
350358Category: Interfaces
351359Category: Qt

Back to history