PDA

View Full Version : QThread confusion, yet another question about quiting thread



Talei
28th July 2010, 07:10
Hello,
I wrote a single piece of code that need to be run in the thread, but I have problem quit'ing the QThread itself.


void MainWindow::on_tb_prev_Refresh_clicked()
{
vFilePrev = new videoFilePreview();
th = new QThread();
vFilePrev->moveToThread( th );

connect( th, SIGNAL(started()), this, SLOT(threadStarted()));
connect( th, SIGNAL(finished()), this, SLOT(threadFinished()));
connect(vFilePrev, SIGNAL(finishedJob()), this, SLOT( vidPreviewFinished()));
connect(vFilePrev, SIGNAL(finishedJob()), th, SLOT( quit()));

th->start();

qDebug() << "gui thread: " << this->thread();
}

void MainWindow::threadStarted()
{
qDebug() << "Start th";
QStringList tmp;
vFilePrev->makePreview( tmp );
}

void MainWindow::vidPreviewFinished()
{
qDebug() << "vidPreviewFinished in thread: " << this->thread();

disconnect(vFilePrev, SIGNAL(finishedJob()), this, SLOT( vidPreviewFinished()));
delete vFilePrev;
}

void MainWindow::threadFinished()
{
disconnect( th, SIGNAL(started()), this, SLOT(threadStarted()));
disconnect( th, SIGNAL(finished()), this, SLOT(threadFinished()));
disconnect( vFilePrev, SIGNAL(finishedJob()), th, SLOT( quit()));

delete th;

qDebug() << "delete th";
}


#include <QObject>
#include <QDebug>
#include <QStringList>

class videoFilePreview : public QObject
{
Q_OBJECT
public:
explicit videoFilePreview(QObject *parent = 0);
void makePreview( QStringList );

private:


signals:
void finishedJob();

public slots:

};



videoFilePreview::videoFilePreview(QObject *parent) :
QObject(parent)
{
}

void videoFilePreview::makePreview( QStringList fileList )
{
qDebug() << "in the thread: " << this->thread();

emit finishedJob();
}

And the problem with this is that connect( th, SIGNAL(finished()), this, SLOT(threadFinished())); never invoke SLOT(threadFinished());

Thanks for any suggestions.
Regards

Talei
28th July 2010, 20:59
SOLVED
Change:

connect( th, SIGNAL( started() ), vFilePrev, SLOT( makePreview() ));
and:

public slots:
void makePreview( );
I would like to know why when I use
connect( th, SIGNAL(started()), this, SLOT(threadStarted()));
the function in this SLOT is run but thread don't seams to react properly (is it because the SLOT is in the same Thread as the QThread, with is GUI thread?).
I saw in the sysinternals that thread is actually created but never quit's, but when I connect actual Thread code to the SIGNA(started()); QThread react properly (at least with this simple example).

franz
29th July 2010, 06:49
I would like to know why when I use
connect( th, SIGNAL(started()), this, SLOT(threadStarted()));
the function in this SLOT is run but thread don't seams to react properly (is it because the SLOT is in the same Thread as the QThread, with is GUI thread?).

Absolutely. connections can be autodetected, queued or direct. If they're directly connected, the slot is executed in the emitting thread, if they're queued the slot is executed in the thread its class lives in. In this case QThread did emit started(), but QThread lives in the main thread, just like your MainWindow. No reason to perform the threadStarted() function in a different thread there.