hashb
1st December 2010, 06:21
Hi, All
I do a testing which use a work thread to access a variable (here is str) which belong to main thread. I found if I add QueuedConnection in connect function ,it will not crash,but without it will crash .
Does anybody know why?
Thanks advance for your help.
#ifndef AAA_
#define AAA_
#include <QThread>
#include <QString>
#include <QDebug>
class myClass : public QThread
{
Q_OBJECT
public:
myClass ()
{
connect(this,
SIGNAL(startSignal()),
this, SLOT(onDoComm())); //will crash
//,Qt::QueuedConnection); //add this parameter will not
}
void run() { exec(); }
void startComm() {emit startSignal();}
signals:
void startSignal();
public slots:
void onDoComm()
{
static long i=1;
qDebug()<<str;
str=QString("%1%2").arg(i++);
emit startSignal();
};
private:
QString str;
};
#endif
#include <iostream>
#include <QApplication>
#include "aaa.h"
using namespace std;
int main(int argc ,char *argv[])
{
QApplication app(argc, argv);
myClass a;
a.start();
a.startComm();
app.exec();
}
I do a testing which use a work thread to access a variable (here is str) which belong to main thread. I found if I add QueuedConnection in connect function ,it will not crash,but without it will crash .
Does anybody know why?
Thanks advance for your help.
#ifndef AAA_
#define AAA_
#include <QThread>
#include <QString>
#include <QDebug>
class myClass : public QThread
{
Q_OBJECT
public:
myClass ()
{
connect(this,
SIGNAL(startSignal()),
this, SLOT(onDoComm())); //will crash
//,Qt::QueuedConnection); //add this parameter will not
}
void run() { exec(); }
void startComm() {emit startSignal();}
signals:
void startSignal();
public slots:
void onDoComm()
{
static long i=1;
qDebug()<<str;
str=QString("%1%2").arg(i++);
emit startSignal();
};
private:
QString str;
};
#endif
#include <iostream>
#include <QApplication>
#include "aaa.h"
using namespace std;
int main(int argc ,char *argv[])
{
QApplication app(argc, argv);
myClass a;
a.start();
a.startComm();
app.exec();
}