
void ThreadTest::exitThread()
{
    // 中断要求を出して、スレッドが完全に終了するのを待機する
    thread->requestInterruption();
    thread->quit();
    thread->wait();
}

void ThreadTest::onExecThread()
{
    if(thread){
        // 既存のスレッドが実行状態なら何もしない
        if(thread->isRunning() == true) return;
        exitThread();
    }

    thread = new QThread(this);
    connect(thread, SIGNAL(started()), this, SLOT(onStartThread()), Qt::DirectConnection);
    connect(thread, SIGNAL(finished()), SLOT(deleteLater()));
    thread->start();
}

void ThreadTest::onStartThread()
{
    while(true){
        // 現在のスレッドで中断要求があればループから抜ける
        if(QThread::currentThread()->isInterruptionRequested() == true) return;
        // スレッド内での処理
    }
}
