[solved] child object working in thread
-
wrote on 20 Sept 2013, 05:08 last edited by
@
//=============================================================================
class myobject: public QObject
{
Q_OBJECT
public:
QTimer *tt;
explicit myobject(QObject *parent = 0);
public slots:
//void myprocess();
void ttevent();
};
//=============================================================================
class mainobject: public QObject
{
Q_OBJECT
public:
explicit mainobject(QObject *parent = 0);
myobject *ob1;
public slots:
//void myprocess();
void mainprocess();
};
//=============================================================================
myobject::myobject(QObject *parent) :
QObject(parent)
{
qDebug() << "Constructor myobject, THREAD :" << QThread::currentThreadId();
tt = new QTimer(this);
connect (tt, SIGNAL(timeout()), this, SLOT(ttevent()));
tt->start(5000);
qDebug() << "Parent of tt is" << tt->parent();
}
//=============================================================================
mainobject::mainobject(QObject *parent) :
QObject(parent)
{
qDebug() << "Constructor mainobject, THREAD :" << QThread::currentThreadId();
ob1 = new myobject(this);
}
//=============================================================================
void mainobject::mainprocess()
{
while(1)
{
qDebug()<<"jai shree ram";
sleep(2);
}
}
//=============================================================================
void myobject::ttevent()
{
qDebug()<<"jai shree krishna";
}
//=============================================================================
//in main function
QThread *Op1 = new QThread;
mainobject *f1 = new mainobject(0);
f1->moveToThread(Op1);
QObject::connect(Op1,SIGNAL(started()),f1,SLOT(mainprocess()));
Op1->start();
//=============================================================================
@
by above code, only jai shree ram is printing every 2 second. jai shree krishna is not printing every 5 second.why? -
Hi,
Because mainprocess is an infinite loop that doesn't let Qt's own event loop run so no signal can be emitted thus no slot are called
-
wrote on 20 Sept 2013, 08:50 last edited by
@
//====================================================
void mythread::run()
{
QTimer *jj = new QTimer(0);
jj->setInterval(1000);
connect(jj,SIGNAL(timeout()),this,SLOT(runevent()));
jj->start();
exec();
while(1);}
//====================================================
void mythread::runevent()
{
qDebug()<<"yahoo";
}
//====================================================
class mythread:public QThread
{
Q_OBJECTprotected:
void run();
public slots:
void runevent();
};
//====================================================
@when i create one thread of mythread in main, "yahoo" will be printed every 1 second inspite of "while(1)"(infinite loop) in run.
i have used event loop exec() for that. but if i want to do same by previous method(movetothread) then what to do? -
In your run method, you don't reach the while statement, exec is blocking, so you have an event loop running and only when ending it (e.g. calling QThread's quit) you'll enter the while loop.
Just create a myobject and move it to a QThread, no need for any while loop in this case
-
wrote on 20 Sept 2013, 11:48 last edited by
myobject is already child of mainobject (see first code). so when i move main object, then my object is autometically move to thread. that i have checked. but timer which is initialized in constructor of my object will never timeout & tt event will never fired.
-
wrote on 20 Sept 2013, 20:07 last edited by
can you show me how to write infinite loop code in thread. qtimer & qnetworkaccessmanager will also be used in thread. i also need events of qtimer & qnetwork accessmanager.
can it possible?
-
The child objects of your moved objects will also get moved, by child object I mean a QObject derivative that has it's parent set the object to be moved.
Putting an infinite loop in a thread that needs qtimer and qnam sounds like a bad design idea. Have a look at the threaded fortune server and client example in Qt's documentation to get an idea on how to start
-
wrote on 21 Sept 2013, 08:21 last edited by
then how to design firmware to achieve my goal. should i use signal & slots? or should i use multiple threads?
-
I can't answer that question, you did not tell what is your exact goal is
-
[quote author="jigar.patel" date="1379751702"]should i use signal & slots? or should i use multiple threads?[/quote]Signals and slots work can work with multiple threads IF you don't use infinite loops (while(1)). If your thread has an infinite loop, it cannot use QTimer and QNetworkAccessManager, because they require signals and slots.
Read http://qt-project.org/doc/qt-5.1/qtcore/qthread.html for examples
-
wrote on 24 Sept 2013, 05:22 last edited by
all of you, thank you very much, for guiding me.
It is very helpfull to me.
now i have successfully used timer & networking in thread. -
You're welcome !
Since you have everything working, don't forget to update the thread title prepending [solved] so other forum users may know a solution has been found :)
-
wrote on 30 Sept 2013, 05:31 last edited by
Finally i have removed infinite loop from my thread main function & i have put it in timer event. so now i can successfully use network management & other timers.
1/13