Change picture:

Choose file:

DaoQt: A Binding to Qt Application and GUI Framework

  1. Hello world
  2. Revised Signal/Slot model
  3. QString
  4. QList and QVector
  5. QTextStream and QFile

The official documentation of Qt4.5 can be found here: Qt Reference Documentation .

With DaoQt module, it is possible to access most of the functionalities of the excellent Qt application and GUI framework. Most of the Qt classes and functions are wrapped as they are, with the same class inheritance relationships and similar function prototypes. However due to the limit of the autobind tool used to generate the bindings, there are several issues one needs to know. These issues will be explained bellow. (This module is released under LGPL!)

1 Hello world

 Top
load  DaoQtCore;
load  DaoQtGui  require  DaoQtCore;

app  =  QApplication('Hello World!');

lab  =  QLabel('Hello World!');
lab.resize(300,  200);
lab.move(200,  200);
lab.show();

app.exec();

2 Revised Signal/Slot model

 Top
  1. Signal/Slot Issues

Using DaoQt, the signals and slots can be connected freely between native Qt objects and objects of derived classes in Dao.
  1. Native Qt object => native Qt object:
    QObject::connect(  sender,  'signal name',  receiver,  'slot name');
    For example,
    win  =  QMainWindow();
    bar  =  win.addToolBar('ToolBar');
    action  =  bar.addAction('Close');
    QObject::connect(  action,  'triggered()',  win,  'close()');
  2. Native Qt object => Dao object:
    QObject::connect(  sender,  'signal name',  receiver,  slot_function  );
    For example,
    class  MyWindow  :  QMainWindow
    {
         routine  MyClose(  a  =  1){  self.close();  }
    }
    win  =  MyWindow();
    bar  =  win.addToolBar('ToolBar');
    action  =  bar.addAction('Close');
    QObject::connect(  action,  'triggered(bool)',  win,  win.MyClose  );
  3. Dao object => native Qt object:
    QObject::connect(  sender,  signal_function,  receiver,  'slot name');
    In this case, the signal can actually be anything except string.
  4. Dao object => Dao object:
    QObject::connect(  sender,  signal_function,  receiver,  slot_function  );
    class  MyObject  :  QObject
    {
         routine  MySlot(  message  ){
              stdio.println('hello:',  message  );
         }
    }

    o  =  MyObject();

    o.connect(  o,  1,  o,  o.MySlot  );
    o.emit(1,  'DaoQt4.5');

    o.connect(  o,  o.MySlot,  o,  o.MySlot  );
    o.emit(  o.MySlot,  'DaoQt4.5');

2.1 Signal/Slot Issues

 Top

  1. In the QObject::connect() function, a string signal/slot always identify a native Qt signal/slot. In other cases, any other objects/data can be used as signal, and only Dao functions can be used as slots;
  2. When connecting native signals or slots with user signals or slots defined in Dao, the native signal and slot names must be complete. Namely, parameter types of the parameter with default values should also be included. For example,
    connect(button,  'clicked()',  daowidget,  daowidget.showMessage  );
    is not valid, because the "clicked" signal of QPushButton has a default parameter. The correct way is,
    connect(button,  'clicked(bool)',  daowidget,  daowidget.showMessage  ).
  3. When connecting a native signal to a Dao slot, the Dao slot must be a function with parameter types matching to that of the signal. If not matched, the slot will not be invoked.

3 QString

 Top

In most cases, if QString is used as parameter or returned value in a function, it is automatically converted to Dao string type when wrapping this function. But if it is used as a pointer, it will not be convered automatically. So for a Qt function with the following prototype:
QString  qt_func(  const  QString  &  p1,  QString  *p2  );
It will be wrapped as,
qt_func(  p1  :  string,  p2  :  QString  )  =>  string;

To use the functionalities of QString, a QString object must be constructed explicitly by calling its constructor, e.g.
s1  =  QString('hi');
s2  =  QString(0x66);

For the methods of the QString class, if the returned value is QString, it is not converted automatically. For example,
QString&  QString::append(const  QString  &s);
will be wrapped as,
append(  self  :  QString,  s  :  string)=>QString
So it will still allow user to write codes like
QString('abc').append('def').append('gh');

To convert from QString to Dao string, one can use toLocal8Bit()

Similarly, in DaoQt, QChar is converted to int, and QByteArray is converted to Dao string.

4 QList and QVector

 Top

Most of the QList<X>, QList<X*>, QVector<X>, and QVector<X*> are converted to list<X>, when they are used as parameters or returned values.

5 QTextStream and QFile

 Top

The operators of QTextStream are not wrapped, but several methods are added for writing data to the stream.
write(  self  :  QTextStream,  data  :  int)=>QTextStream
write(  self  :  QTextStream,  data  :  float)=>QTextStream
write(  self  :  QTextStream,  data  :  double)=>QTextStream
write(  self  :  QTextStream,  data  :  string)=>QTextStream
write(  self  :  QTextStream,  data  :  any  )=>QTextStream

The QFile.close() must be called when it is done.
view count 1064 times
created at 2009-06-27, 10:49 GMT
modified at 2009-06-27, 12:37 GMT

12 3
456789 10
111213141516 17
181920212223 24
2526272829 30 31

fu: Many thanks (Jul.04,04:29)

klabim: fixed Hi, great, now my test works now :- ). (Jun.30,17:51)

Nightwalker: Few suggestions (Jul.03,14:37)

This site is powered by Dao
Copyright (C) 2009,2010, daovm.net.
Webmaster: admin@daovm.net