Pages

Monday, March 14, 2005

Learnings in the project Part 1 : QT learnings

QCANVAS
The most important learning was perhaps QCanvas/QScrollview/QCanvasView

It you are looking for having a GUI in which you can put various items(including image as i did), QCanvas might be good for you. Look in to the
code for the learning how to use it.


QPROCESS
I used QProcess for the first time in this project. I would say its a wonderfull Class and if you use plan to use system call in ur QT programs
you should give QProcess a try.

QProcess basically allows calling any process(a program) from within a QT program. Assume that you wanted to call a program as a system call you
might do it like



QString qstr="./executable arg1 arg2 arg3";
char *temp=new char [100];

strcpy(temp,qstr.toLatin1()));
system(temp);


of course conversion to char will not be required if you use sprintf etc. Now doing the same using QProcess will be achieved as follow:


QProcess *qproc=new QProcess(this);
qproc->AddArguments("./executable");
qproc->AddArguments("arg1");
qproc->AddArguments("arg2");
qproc->AddArguments("arg3");

qproc->start();


Now the question in ur mind is why use QProcess? when system is available, Here are the reasons
1) QProcess will actually fork the process and not hang when called as system will do.
2) QProcess allows you setting 'slots' to handle the 'signals' callbacks send over event triggers of the QProcess object. The important ones
are QProcess::processExited,QProcess::readyReadStdout/readyReadStderr etc.
3) QProcess allows you interaction with the program by allowing you to write to the stdin and reading stdout, this functionality is provided
by slots like QProcess::writeToStdin.
4) You can kill the program in between using kill, or tryTerminate function calls.
5) you can keep updating the GUI while the sytem call is being processesed.

Now there are some things that I used specially for my program.
1) I wanted to wait till the process exited before doing the further processing. For this If I just run a simple while loop while process `isRunning` the GUI will freeze, so on someones' suggestion in the qt-interest mailing list I added this qApp->processEvents to my code.

while(proc->isRunning()){
qApp->processEvents();
...
}


2) I needed to start a program "gnome-obex-server" and then kill it later both on call of some functions so I simply, used the tryTerminate and
kill function with a QTimer:

proc->tryTerminate();
QTimer::singleShot(3000,proc,SLOT(kill()));


QTimer
This is another wonderful thing I came to know during this GUI development. Basically QTimer allows timely/repeatitive calls of functions.
I used QTimer to write a function to regularly check if some new files are added to the $HOME where gnome-obex-server stores the files sent
over bluetooth.

timerBt = new QTimer( this );
connect( timerBt, SIGNAL(timeout()), this, SLOT(timerDone()) );
timerBt->start( 30000, FALSE );



And my timerDone function had a code like this:

void MyObjectWindow::timerDone(){
bool insert=false;
int count=0;
QDir d1("/home/vardhman");
QStringList lst = d1.entryList( "*.jpg" );
for ( QStringList::Iterator it = lst.begin(); it != lst.end() &&count<5 insert =" true;" st="d1.absPath()+" i="0;i<" l="strlen(st.latin1());" insert="false;">

[Can refer to the complete code here].

No comments:

Post a Comment