PDA

View Full Version : Problem using signals and thread



aarelovich
27th June 2008, 23:38
Hello, I have the following problem:
I have to execute a long algorithm when a user pushes a button. So I created a new class that is a subclass from QThread and use the button push to start said thread. But I also want to have some sort of measure of the progress and for that I wanted to create a signal and a slot. So I made a little test program. However when I used connect, to connect my slot and signal I get a console message (I'm compiling in debug mode):

Object::connect: No such signal QThread:: p(int)
Object::connect: (receiver name: 'ConnectTryClass')

Where p is the name of my signal in my QThread subclass.

Here is the code for the Sender class (.cpp) (QThread decendant):


#include "Sender.h"

Sender::Sender()
{
}

void Sender::run(){
int N = 10000000;
int a;
for (int i = 0; i < N; i++){
a++;
double val = (double)(i+1)*100/double(N);
emit p((int)(val));
}
}

void Sender::p(int val){
}

Sender::~Sender()
{
}


and its header:


class Sender: public QThread
{
public:
Sender();
void run();
~Sender();

signals:
void p(int val);

};


And where I call the connec (ConnectTry.cpp)t:


#include "connecttry.h"

ConnectTry::ConnectTry(QWidget *parent): QMainWindow(parent)
{
ui.setupUi(this);
sender = new Sender();
bool res = connect(sender,SIGNAL(p(int)),this,SLOT(on_p(int)) );
if (res)
ui.teLog->append("Yes");
else{
ui.teLog->append("No");
}
}

void ConnectTry::on_p(int val){
ui.pbDone->setValue(val);
}

void ConnectTry::on_pbTest_clicked(){
sender->start(QThread::InheritPriority);
ui.teLog->append("Done");
}


Can any one tell me what I'm doing wrong?
Thank you

jacek
28th June 2008, 00:02
You forgot the Q_OBJECT macro.

aarelovich
30th June 2008, 13:57
Thank you very much that effectively solved my problem