PDA

View Full Version : how to set wait condition



hema
25th July 2011, 05:25
hi,

i have some ballons which should come one by one with a time gap.so that i can get random character different as ballon text.how can i do that?

please help me

regards,
hema

Santosh Reddy
25th July 2011, 06:42
I guess you mean tool tip by balloons. You could use QTimer to do so, start a QTimer, and in the slot connected to QTimer show the balloons as required.


QTimer * timer = new QTimer(1000); // 1 second
connect(timer, SIGNAL(timeout), this, SLOT(createBalloon));
timer->start();

void createBalloon(void)
{
//create / show balloon.
}

I hope, you know how to use signals and slots

hema
25th July 2011, 07:03
hi sir,
thanks for ur reply.
the solution giving error.
main.cpp-------------where i call ballon.cpp

#include<QtGui>
#include"ballon.h"
int main(int argc,char **argv)
{

QApplication app(argc,argv);
QGraphicsScene scene;
QString str[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
scene.setSceneRect(0,0,600,600);
scene.addRect(0,10,600,30);
scene.addText("After / Before",QFont("",18,QFont::Bold));
scene.setBackgroundBrush(Qt::yellow);
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
for(int i=1;i<=3;i++)
{
for(int j=1;j<=9;j++)
{
if(i*j<=26)
{
QTimer *interval=new QTimer(1000,this);
QObject::connect(&interval,SIGNAL(timeout()),this,SLOT(createBallon( )));

}

}
}

QGraphicsView view(&scene);
view.setRenderHint(QPainter::Antialiasing);
view.setCacheMode(QGraphicsView::CacheBackground);
//view.setMaximumSize(600,600);
view.setViewportUpdateMode(QGraphicsView::Bounding RectViewportUpdate);
view.show();
QTimer timer;
QObject::connect(&timer,SIGNAL(timeout()),&scene,SLOT(advance()));
timer.start(1000);
return app.exec();
}
void createBallon()
{
int random=qrand()%26;
Ballon *ball=new Ballon(str[random]);
ball->setPos(0+(j*60),550+(i*65));
scene.addItem(ball);

}



ballon.cpp -----------------


#include "ballon.h"
#include<QFont>
#include<QPainter>
#include<QGraphicsItemAnimation>
#include<QGraphicsScene>
#include<QGraphicsSceneMouseEvent>

Ballon::Ballon(QString text): _mtext(text)
{
setFlag(QGraphicsItem::ItemIsMovable,TRUE);

}
void Ballon::setText(QString text)
{
_mtext=text;
}
QString Ballon::getText()
{
return _mtext;
}
void Ballon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setBrush(Qt::red);
painter->drawEllipse(boundingRect());
painter->drawText(boundingRect().center(),_mtext);

}

QRectF Ballon::boundingRect() const
{
return QRectF(0,0,40,60);
}
void Ballon::advance(int phase)
{
Q_UNUSED(phase);
QList<QGraphicsItem*> items=scene()->items();
foreach (QGraphicsItem *item, items) {
QPointF point=item->pos();
if(point.ry()<=40)
item->setPos(point.rx(),point.ry());
else
item->moveBy(0,-2);
}
}
void Ballon::mouseDoubleClickEvent(QGraphicsSceneMouseE vent *event)
{
QGraphicsItem *item=scene()->mouseGrabberItem();
scene()->removeItem(item);

}



Error-----------invalid use of this in non member function
how can i set time interval please how can i insert signal from main.cpp to ballon constructor

Santosh Reddy
25th July 2011, 07:15
Note my earlier reply
I hope, you know how to use signals and slots
You get an error because you don't know how to use signals & slots (Don't feel bad). I will try to explain a bit, you should be reading a good book or Qt Documentation at-least.

Following points are what you missed
- main() is a global function, it does not have a object instance of itself, so you cannot use this pointer in main() (it's basic C++, nothing to do with Qt), this is very reason you have an error.
- A slot has to a member function of a class which is either directly or indirectly derived from a QObject, i.e SLOT cannot be a global function (which is a problem in you case)
- You are creating 27 (3 x 9) timers, you should be creating only one timer, and each time the timer slot is called then create a balloon.

hema
25th July 2011, 07:31
sir,

i know that 'this' can not be used in main function.

my app is to produce 26 ballons with A-Z as text . ballon,GraphicsItem should be viewed in GraphicsScene with a time gap.

how can i achieve this?

i have to call ballon constuctor with random character as input in main.cpp.

regards,
hema

Santosh Reddy
25th July 2011, 08:08
Ok, there are many ways, i just give one example to do so. Refer the sample example, I tried to create from your inputs, check it out