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:
worker = new Worker;
worker->moveToThread(thread);
connect(this, SIGNAL(mysignal()),worker, SLOT(doWork()));
thread = new QThread();
worker = new Worker;
worker->moveToThread(thread);
connect(this, SIGNAL(mysignal()),worker, SLOT(doWork()));
To copy to clipboard, switch view to plain text mode
in worker.cpp:
void Worker::doWork()
{
for (int i = 0; i <=200000; ++i)
{
qDebug() << i;
QTimer::singleShot(1000,
&loop,
SLOT(quit
()));
// this should wait 1 sec like a sleep(1) loop.exec();
}
}
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();
}
}
To copy to clipboard, switch view to plain text mode
in wp1.cpp I overwrite keypressed to emit the signal:
void WP1
::keyPressEvent(QKeyEvent *event
) // definition {
switch(event->key())
{
case Qt::Key_1:
qDebug() <<" before";
emit(mysignal());
qDebug() << "after";
break;
}
}
void WP1::keyPressEvent(QKeyEvent *event) // definition
{
QWidget::keyPressEvent(event);
switch(event->key())
{
case Qt::Key_1:
qDebug() <<" before";
emit(mysignal());
qDebug() << "after";
break;
}
}
To copy to clipboard, switch view to plain text mode
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!
Bookmarks