PDA

View Full Version : QThread + signal problem



BIllNo123
24th August 2010, 16:07
I've got the code which crashes:

//MyThread.h file
#include <QtGui>
#include <iostream>


class MyThread : public QThread
{
Q_OBJECT

public:
MyThread ();
void run();

signals:
void Signal_AppendTextThread(const std::string &text);


protected slots:
void Slot_ShowMessage(const std::string &);

};


//MyThread.cpp file
#include "MyThread.h"


MyThread::MyThread(): QThread()
{
}

void MyThread:Slot_ShowMessage(const std::string &text)
{
}

void MyThread::run()
{

for(int i=1;i<1000;i++)
{
emit Signal_AppendTextThread("emitting thread signal ");
}

exec(); //do i really need this?
}


// MyMainWindow.h file

#include <QtGui/QMainWindow>
#include <QtGui/QApplication>
#include <QtGui/QLineEdit>
#include "MyThread.h"



class MyMainWindow: public QMainWindow
{
Q_OBJECT

public:
MyMainWindow( QWidget * pParent = 0, int argc = 0, char **argv = 0 );
~MyMainWindow();

MyThread m_pApplyAlgorithmThread;
QTextEdit *m_pTextEdit;

protected slots:
void Slot_ShowMessage(const std::string &);

};



//MyMainWindow.cpp file

#include "MyMainWindow.h"

MyMainWindow::MyMainWindow( QWidget * pParent, int argc, char **argv ) :
QMainWindow( pParent )
{

setWindowTitle( "Thread Test" );

m_pTextEdit = new QTextEdit(this);
m_pTextEdit->setReadOnly(true);
m_pTextEdit->setGeometry(0,0,200,200);


m_pApplyAlgorithmThread.start();

connect(&m_pApplyAlgorithmThread, SIGNAL(Signal_AppendTextThread(const std::string &)),
this, SLOT(Slot_ShowMessage(const std::string &)),Qt::DirectConnection );


showMaximized();

for(int i=1;i<1000;i++)
{
m_pTextEdit->append("main");
m_pTextEdit->repaint();

//this->thread()->wait(100);
}
}

MyMainWindow::~MyMainWindow()
{
}

void MyMainWindow::Slot_ShowMessage(const std::string &text)
{
m_pTextEdit->append(text.c_str());
m_pTextEdit->repaint();
}


What am i doing wrong?

Lykurg
24th August 2010, 16:16
where does it crash and what is the error message? And there are also
tags on the forum.

BIllNo123
24th August 2010, 16:22
I use VS 2008 in WIndows XP 64 bit. I run it in release mode so i don't have much debug info available.
I noticed that if i set a smaller number in for-loops (e.g. 10), it doesn't crash.
I'm new in QThread. Qt::DirectConnection is ok? or should i use QueuedConnection?
Sorry, but i'm very confused.

qt_gotcha
24th August 2010, 16:22
I found this very helpful, maybe it gives you some ideas:
http://thesmithfam.org/blog/2009/09/30/lock-free-multi-threading-in-qt/

Lykurg
24th August 2010, 16:30
I use VS 2008 in WIndows XP 64 bit. I run it in release mode so i don't have much debug info available.Then you maybe want run in application in debug mode...

Talei
25th August 2010, 01:21
I found this very helpful, maybe it gives you some ideas:
http://thesmithfam.org/blog/2009/09/30/lock-free-multi-threading-in-qt/
Don't use moveToThread(this); in QThread class. See: http://labs.qt.nokia.com/blogs/2010/06/17/youre-doing-it-wrong/

BIllNo123
25th August 2010, 10:20
Thanks everyone!
I write the solution to my problem here:
I had to change the connection type from direct to Queued connection (thread safe) and then register the data type (std::string) i pass through the signal-slot mechanism (the registration is obligatory for queued connections).