PDA

View Full Version : is it possible to stay on a user defined infinite loop?



mahe2310
23rd March 2006, 11:50
Is it possible for a qtopia application to stay on an infiniteIloop from the time of its loading?

When i called the loop in the constructor the window itself is no loading....

Mahe2310

high_flyer
23rd March 2006, 13:03
if you are calling an infinite loop in your constructor, ofcourse the widgwt never gets contructed...
But it could be I missunderstood you.
Post the code.

Ben.Hines
23rd March 2006, 17:17
Doing an infinite loop in your constructor wouldn't work. The constructor needs to return. Assuming that the object being constructed is a QObject-derivative, you could do use the QTimer::singleShot method to allow the constructor to return and yet still call the method with your infinite loop:



class MyWidget : public QWidget
{
Q_OBJECT
public:
// Constructor
MyWidget(QWidget *parent);

private slots:
void MyInfiniteLoop();
};

MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
// Do whatever you need to do here.

// Use a zero-timeout single shot timer here to allow the
// constructor to exit before MyInfiniteLoop gets called.
QTimer::singleShot(0, this, SLOT(MyInfiniteLoop()));
}

void MyWidget::MyInfiniteLoop()
{
while(true)
{
// Do something interesting
}
}

high_flyer
23rd March 2006, 20:13
this is bad design, both methods.
You should never call an infinite loop in a constructor.
make the loop in a public method/slot and call normally after construction.
There can be no reason for calling an infinite loop in a constructor.
And in a Qt program, if you use an infinite loop, it would be best to use a QThread obeject to house it in its run() method.

mahe2310
24th March 2006, 03:57
Doing an infinite loop in your constructor wouldn't work. The constructor needs to return. Assuming that the object being constructed is a QObject-derivative, you could do use the QTimer::singleShot method to allow the constructor to return and yet still call the method with your infinite loop:



class MyWidget : public QWidget
{
Q_OBJECT
public:
// Constructor
MyWidget(QWidget *parent);

private slots:
void MyInfiniteLoop();
};

MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
// Do whatever you need to do here.

// Use a zero-timeout single shot timer here to allow the
// constructor to exit before MyInfiniteLoop gets called.
QTimer::singleShot(0, this, SLOT(MyInfiniteLoop()));
}

void MyWidget::MyInfiniteLoop()
{
while(true)
{
// Do something interesting
}
}




thank you,
That is a good method.

Mahe2310

high_flyer
24th March 2006, 12:05
@Mahe2310
Could you explain why it is you need an infinite loop in a costructor?
I am sure if you explain what it is you want to acheive it can be done in a better way.

mahe2310
24th March 2006, 12:17
@Mahe2310
Could you explain why it is you need an infinite loop in a costructor?
I am sure if you explain what it is you want to acheive it can be done in a better way.


Hi High_Flyer,

Myself want to handle the qtopia key events and some message queue events simultaneously.
I mean, handle them always. So I thought of going for an infinite loop which check for a qt event first and then message event.

Is it possible to call the MyWidget::event(QEvent*)?

I will show how the loop should look like...



while(1)
{
if(true == event(QEvent *evt) ) /* keyEvent detected */
{
MyEventHandler(event);
}
if( /* Messgae at Queue */ )
{
/* Handle the Message */
}
}

jpn
24th March 2006, 12:31
And how do you expect to receive events if you are inside an infinite loop?
How about just processing events in a "normal way"? By subclassing or using an even filter, that is..

mahe2310
24th March 2006, 13:43
And how do you expect to receive events if you are inside an infinite loop?
How about just processing events in a "normal way"? By subclassing or using an even filter, that is..


i didnt get that...
can u explain it...

mahe2310

high_flyer
24th March 2006, 14:29
But why do you want to start your endless loop in the contructor and not just call it normally?
But even so it would not work.
What jpn means is, if the main event loop is stuck in your endless loop, it will never have time to attend the events.
You should use an event filter.
http://doc.trolltech.com/4.1/qobject.html#installEventFilter

The above link is to the normal Qt4, but look the same in QTopia docs, I think it should be the same or very similar.