PDA

View Full Version : Seems that my emit doesn't emit the signal



roseicollis
19th January 2015, 15:43
Hi,
I'm making a GUI app with the wizard and wizardpages structure and I need to do some background heavy work in some cases so I’ve created the Worker class inheriting from QObject. Then, supposing I need to do some work in wp1 which is the first wizardpage, I have:

in wp1.cpp constructor:

thread = new QThread();
worker = new Worker;
worker->moveToThread(thread);

connect(this, SIGNAL(mysignal()),worker, SLOT(doWork()));
in worker.cpp:

void Worker::doWork()
{
for (int i = 0; i <=200000; ++i)
{
qDebug() << i;

QEventLoop loop;
QTimer::singleShot(1000, &loop, SLOT(quit())); // this should wait 1 sec like a sleep(1)
loop.exec();

}
}

in wp1.cpp I overwrite keypressed to emit the signal:

void WP1::keyPressEvent(QKeyEvent *event) // definition
{
QWidget::keyPressEvent(event);
switch(event->key())
{
case Qt::Key_1:
qDebug() <<" before";
emit(mysignal());
qDebug() << "after";
break;
}
}
My problem is that it prints ‘before’ and ‘after’ but seems that the signal is not emited so it never gets to the function worker::doWork()… Any idea of why?
(I put the emit in keypressed just to control when I emit it to test if worker functions as I expect)

Thank you!

wysota
19th January 2015, 16:02
Did you start the thread? Did you run its event loop?

roseicollis
19th January 2015, 16:05
ohhh shit... yes that was... forgot to thread.start();

thank you!