Emit signal from QThread to main thread!!!
Hi
I have tried to create thread.
my doubts are as below:
1.the way it is created is correct or not?
2.After adding signal Getting error in .h file Signal_FromQThread(QString) is protected and in .cpp giving error "within context" where signal is emitted.
3.the purpose of creating QThread is to do calculations in this thread and after passing control to main thread slot update() is called for paint.so that time it is taking now can be reduced.
pls refer below code
pls suggest on it
Thanks,
sanujas
//in thread.h
#include <qthread.h>
class Cthread : public QWidget, private Ui::CthreadBase
{
Q_OBJECT
//Qthread newly added
class CRefresher : public QThread
{
public:
static Cthread *m_fun;
signals:
void Signal_FromQThread(QString);
protected:
void run()
{
m_fun->updateThefun();
}
};
friend class CRefresher;
private slots:
void updatefun();
private:
void updateThefun();
}
//in thread.cpp
//in constructor
Cthread* Cthread::CRefresher::m_scopeGraph = NULL;
Cthread::Cthread( QWidget *l_Parent ) : QWidget( l_Parent )
{
updateThefun();
Cthread::CRefresher::m_fun = this;
m_TimerThread = new QTimer( this );
connect( m_TimerThread, SIGNAL( timeout() ), this,
SLOT( updatefun()) );
connect(Cthread::CRefresher::m_fun, SIGNAL(Signal_FromQThread(QString)), this, SLOT(update()), Qt::QueuedConnection);
m_TimerThread->start( 1000 );
}
//newly added function
void Cthread::updatefun()
{
Cthread::CRefresher l_refersher;
l_refersher.start();
l_refersher.setPriority(QThread::LowestPriority);
l_refersher.wait();
emit l_refersher.Signal_FromQThread("emitting thread signal");
}
//in function
void Cthread::updateThefun()
{
//some calculations
//....
//....
update();
}
void Cthread::paintEvent( QPaintEvent *f_ptrEvent)
{
//draw
//....
//....
}
Re: Emit signal from QThread to main thread!!!
You cannot access widgets from within threads.
Re: Emit signal from QThread to main thread!!!
The code snippet is seriously broken.
There is a widget subclass declaration that is unfinished and weirdly called Cthread, there is a QThread subclass declaration with a signals section but no Q_OBJECT macro, the thread's calls into the widget instead of doing the calculation as the introduction text claims it would, and so on.
I suggest you repost the code, using code tags and separate section for the two classes.
The code tags are like the quote tags, just with the keyword code instead of quote.
Cheers,
_