Difference between revisions of "Qt4 binding"

From Free Pascal wiki
Jump to navigationJump to search
(wikified link to lazarus qt information)
 
Line 3: Line 3:
 
*[http://users.pandora.be/Jan.Van.hijfte/qtforfpc/fpcqt4.html Pascal Qt4 binding home page]
 
*[http://users.pandora.be/Jan.Van.hijfte/qtforfpc/fpcqt4.html Pascal Qt4 binding home page]
 
*[http://users.pandora.be/Jan.Van.hijfte/qtforfpc/qtedemo.html Pascal Qt/E binding home page]
 
*[http://users.pandora.be/Jan.Van.hijfte/qtforfpc/qtedemo.html Pascal Qt/E binding home page]
*[[lazarus:Qt_Interface#Qt_4_Bindings Lazarus|Qt4 interface]]
+
*[[lazarus:Qt_Interface#Qt_4_Bindings|Lazarus Qt4 interface]]
  
 
== How to use the Qt4 binding ==  
 
== How to use the Qt4 binding ==  

Revision as of 11:28, 13 February 2006

Links

How to use the Qt4 binding

Start from the demo program

  • The demo program shows you the basic framework of a minimal program.
  • You may also take a look at the currently more advanced Qt/E demo program.


Expand by using the Qt Documentation

How to create a Class

Look up the Qt Class you would like to use. Suppose it is QWidget. 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.

var W : QWidgetH;
...
W := QWidget_create();


How to call a method of a Qt Class

Use the Trolltech Qt documentaton to find out how and when to use which method. Suppose you want to use "void resize ( int w, int h )" The corresponding Pascal fuction will be ClassName+_+MethodName. So look in Qt4.pas for the definition of QWidget_resize

procedure QWidget_resize(handle: QWidgetH; w: Integer; h: Integer);

Note how the pascal function has an additional parameter: the Qt Class instance handle of your QWidget Class. Calling the resize method will be like this

QWidget_resize(W,100,200);


C++ Multiple inheritance

Some 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:

function QWidget_to_QPaintDevice(handle: QWidgetH): QPaintDeviceH;

In 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

var PaintBox : QWidgetH;
PaintBox:=QWidget_create();
...
 // In the paint method
var CV : QPainterH;
CV:=QPainter_create(QWidget_to_QPaintDevice(PaintBox));

Do not use typecasting like QPaintDeviceH(PaintBox). The compiler would complain if you did this anyhow. See type safety below.

Technical information

Type safe Qt Class Handles

In Qt4.pas you will see a large list of type defintions of Qt Class handles.

QWidgetH = class(QObjectH) end;
  QAbstractButtonH = class(QWidgetH) end;
    QPushButtonH = class(QAbstractButtonH) end;

This list ensures type safety and encodes the class hierarchy.

Inheritance

This 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

var Btn : QPushButtonH;
...
Btn := QPushButton_create(parent);
writeln('Button Width:',QWidget_width(Btn));

Type Safety

In 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

// Do not do this !!!
CV:=QPainter_create(PaintBox);

The type safety net will make the compiler complain like this:

demo.pas(265,30) Error: Incompatible type for arg no. 1: Got "QWidgetH", expected "QPaintDeviceH"

Similar if you would not use the convert function, but would just use typecast like this

// Do not do this !!!
CV:=QPainter_create(QPaintDeviceH(PaintBox));

The compiler will complain with:

demo.pas(266,21) Warning: Class types "QWidgetH" and "QPaintDeviceH" are not related

That 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. Media:Example.mp3