PDA

View Full Version : Image showing



nmkarvekar
11th December 2009, 08:06
Hi everybody,

I have following scenerio...

show image
wait for some milisecond say 500 ms
again show image
wait for some milisecond say 500 ms
again show image
...

I m using QT-3.1 on Fedora Linux and written following code for showing image on QT Widget.

fm->setPaletteBackgroundPixmap(QPixmap("sample1.jpg"));
QApplication::eventLoop()->processEvents(QEventLoop::AllEvents, 100);
usleep(500);
fm->setPaletteBackgroundPixmap(QPixmap("sample2.jpg"));
QApplication::eventLoop()->processEvents(QEventLoop::AllEvents, 100);
usleep(500);
fm->setPaletteBackgroundPixmap(QPixmap("sample3.jpg"));
QApplication::eventLoop()->processEvents(QEventLoop::AllEvents, 100);
usleep(500);

Its working ok, but after say some hours, or some days application stucks on processEvents and won't work until i restart the appl or reboot the machine.


How could i resolve this issue? My application needs to work 24*7.

Hope everybody got my problem.

Thanks in advance.

-Niranjan.

high_flyer
11th December 2009, 11:25
EDIT:
wont it be better to connect a timer to a slot that show the image every 500ms?

nmkarvekar
14th December 2009, 07:59
Thanks for your reply.

I tried with QTimer including following code in form1.ui.h .

void frmMain::timerSlot()
{
fm->setPaletteBackgroundPixmap(QPixmap(objGB.imgName)) ;
}

and called by slot in the function like included in class clsGB
void clsGB::showScr()
{
QTimer::singleShot(500,fm,SLOT(timerSlot()));
}


and in the main.cpp i called like
class clsGB objGB.
objGB.imgName="sample1.jpg"
objGB.showScr();
objGB.imgName="sample2.jpg"
objGB.showScr();
objGB.imgName="sample3.jpg"
objGB.showScr();
objGB.imgName="sample4.jpg"
objGB.showScr();
............

It will directly show sample4.jpg. I want like, show sample1.jpg then wait for 500 ms tyhen show sample2.jpg..... etc.


Is anything other than QTimer or can i write different code for showing image timerslot.

Thanks in advance

-Niranjan

high_flyer
14th December 2009, 15:28
I don't quite understand what it is you have written but the following code should show another image every 500 ms:


//in frmMain.h you add:
int m_iImage;

//in the constructor you add:
m_iImiage = 1;

//and then in your slot:
void frmMain::timerSlot()
{
QString strImageName;

switch(m_iImage)
{
case 1: strImageName = "sample1.jpg"
break;
case 2: strImageName = "sample2.jpg"
break;
case 3: strImageName = "sample3.jpg"
break;
case 4: strImageName = "sample4.jpg"
break;
case 5: strImageName = "sample5.jpg"
m_iImage = 0;
break;
}
fm->setPaletteBackgroundPixmap(QPixmap(strImageName ) );
m_iImage++;
}


This is just variant of doing this, there may be others too.

nmkarvekar
15th December 2009, 06:23
Hi,

Thanks for your reply. Your idea is correct if i don't use singleshot QTimer.
But in my scenerio, i want to use singleshot timer.

I written your code in the timerslot and called slot like:
QTimer *t = new QTimer(fm);
connect( t, SIGNAL(timeout()), SLOT(timerSlot()) );
t->start( 500, false );


Its working fine and i can see images after 500 ms. But actually in my case, i want to use singleShot timer, because its not fixed when i want to show specfic image, its depends on specific event. Like following:


The main objective in my case is, Image should stay on widget for some interval on perticular event. If any other event occurs then again call image function show other image and wait..

Algorithm for the problem:

-------EVENT STARTS---------
show image
wait for 500 ms
-------EVENT COMPLETED-----







I Hope, you are getting my problem.


-Niranjan.

high_flyer
15th December 2009, 11:23
Well, then install an event filter, and set the timer interval for each event as you want.

nmkarvekar
15th December 2009, 16:15
Hi,

Thanks for your reply.

Can you give me sample code, to do this? Let me give one example so that you can understand my scenerio.

/////////////////main.cpp file start///////////////////////////////////////////////////////////////////////
#include <qapplication.h>
#include "form1.h"
#include "AxSample.h"
#include <qeventloop.h>
#include <qsplashscreen.h>
#include <qpixmap.h>
class Form1 *fm;
class AxSample objGB;
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
Form1 w;
fm=&w;
w.show();
objGB.imgName="sample1.jpg";
objGB.scrFormat();
objGB.imgName="sample2.jpg";
objGB.scrFormat();
objGB.imgName="sample3.jpg";
objGB.scrFormat();
objGB.imgName="sample4.jpg";;
objGB.scrFormat();
objGB.imgName="sample5.jpg";
objGB.scrFormat();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
////////////////////main.cpp file end//////////////////////////////////////////////////////////////////////////////



//////////////AxSample.cpp start/////////////////////////////////////////////////////////////////////////
#include "form1.h"
#include "AxSample.h"
#include <qtimer.h>
#include <semaphore.h>
#include <signal.h>
#include <qpixmap.h>
#include <qdatetime.h>
extern Form1 *fm;

AxSample::AxSample()
{
}

AxSample::~AxSample()
{
}
void AxSample::scrFormat()
{
QTimer::singleShot(500,fm,SLOT(showImg()));

}
//////////////AxSample.cpp end/////////////////////////////////////////////////////////////////////////


///////////AxSample.h start////////////////////////////////////////////////////////////////////////////////////////////

#include <qstring.h>
#include <qthread.h>
class AxSample
{

public:
AxSample();
~AxSample();
QString imgName;
void scrFormat();
};

///////////////AxSample.h end///////////////////////////////////////////////////////////////////////////////////////


//////////form1.ui.h start////////////////////////////////////////////////////////////////////////////

#include "form1.h"
#include "AxSample.h"
#include <semaphore.h>
#include <qtimer.h>
#include <signal.h>
#include <qsplashscreen.h>
extern Form1 *fm;
extern AxSample objGB;
void Form1::showImg()
{

fm->setPaletteBackgroundPixmap(QPixmap(objGB.imgName)) ;

}
//////////////form1.ui.h end///////////////////////////////////////////////////////////////////////////////////////////


Now If run this example, i can see only sample5.jpg and thats obivious as QTimer::singleshot is asynchronus function. The above is just a sample for scenerio.In actual code, i call scrformat function on different events or states with image name is diffeernt on each state. Now If you look the above code, how can we show sample1.jpg, then wait.. then sample2.jpg then wait....

I guess i need some blocking or synchronous function so that after showing image & wait 500 sec i can call second scrformat function


Thank you.

-Niranjan.

high_flyer
16th December 2009, 12:42
Here is example code:
http://doc.trolltech.com/4.5/qobject.html#installEventFilter

nmkarvekar
17th December 2009, 08:01
Hi,

Thanks for the example. From the above my example, how can we make image showing and waiting for some interval .

Thanks in advance.

-Niranjan.

high_flyer
17th December 2009, 10:00
I showed you already, and you can also see the example code for catching events.

nmkarvekar
17th December 2009, 11:52
Hi,

Thanks for your reply.

Actually my problem is not about catching events. I already catched the events. Only problem is how to display image and wait. I tried several ways like:

1st way i tried is:::

//To display image on widget
void frmMain::showImage()
{
fm->setPaletteBackgroundPixmap(QPixmap(imgName));
QApplication::eventLoop()->processEvents(QEventLoop::AllEvents, 100);
usleep(500);
}

//As new states comes , it will come in this function to show image and do work of state
void frmMain::nextState(QString id)
{
if(id=="A")
{
//show image of state A
imgName=sample1.jpg;
showImage();
...........
}
else if(id=="B")
{
//show image of state B
imgName=sample2.jpg;
showImage();
.........
}

}

////////////////main.cpp//////////////////////////////////////
.......
...
fm->nextState("A");
fm->nextState("B");
.....
////////////////////////////////////////////////


In above now, it workd fine, i can see sample1.jpg,then wait for 500 ms, then sample2.jpg and so on. but sometimes it stucks on QApplication.


2nd way i tried is using QTimer like:::::::::::


void frmMain::showImage()
{
fm->setPaletteBackgroundPixmap(QPixmap(imgName));

}

void frmMain::nextState(QString id)
{
if(id=="A")
{
//show image of state A
imgName=sample1.jpg;
QTimer::singleShot(500,fm,SLOT(showImage()));
}
else if(id=="B")
{
//show image of state B
imgName=sample2.jpg;
QTimer::singleShot(500,fm,SLOT(showImage()));
}

}

////////////////main.cpp//////////////////////////////////////
fm->nextState("A");
fm->nextState("B");
.....
////////////////////////////////////////////////

In 2nd way, i can see sample2.jpg only and thats obivous as QTimer is asnychronus function.


If anything other that it please let me know.

Thank you.

-Niranjan.