Nominate our 2022 Qt Champions!

QThread terminate

  • Hi everyone,

    I want to terminate QThread process while it is running. The running process is one action and takes too much time (ex. opening an image which size 1GB). When the main thread shows progress dialog, our worker thread try to open this image. And if the user clicks the cancel button on progress dialog, we want to terminate this process.
    The worker thread calls some 3rd party library functions. It means that we can't add any signal to this code and also we don't have an event loop in the run function. QThread::terminate() function doesn't work in this situation. Do you have any idea how we can terminate it?

    QThread* worker;
    connect(progressDialog, &ProgressDialog::canceled, [ this, worker ] () {
        if (!worker->isRunning())      qDebug() << "thread is stopped";

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What library are you using to load that image ?

  • Hi SGaist,
    I use OpenCascade 6.9.1 for this process. The problem is we couldn't terminate worker thread, while it is working.

  • Lifetime Qt Champion

    What exactly are you using from them ?

  • Actually opening image just a sample example in my case for better understand. Let me tell the correct case.
    We are opening iges file and demonstrating it on Opencascade. For this process, we call IGESControl_Reader class. IGESControl_Reader::TransferRoots() function create OCC shapes after reading the iges file. If the iges file is too big, this process takes 4-5 minutes. When this function is completed successfully, it returns a TopoDS_Compound object which contains all shapes.
    If the user don't want to wait for 4-5 minutes, he/she just click the "Cancel" button on ProgressDialog, and then the process will be terminated. Because creating all OCC shapes is managed by a worker QThread, if we terminate this thread, the process will be cancelled.

    We can give more examples related to this issue. For example, it could be reading matrix in a file and then calculate inverse matrix with using one of the math library. If we don't have a chance to modify library source code, how could it be terminated this process by QThread?

    Because of that reason, I tried upper code block but it doesn't work. Is there any illogical situation?

  • Lifetime Qt Champion

    In that case you should contact the OpenCascade folks to see if there's an alternative to TransferRoots that transfers one root at a time and that you can then handle in a cancelable for loop or if they provide directly a way to cancel the operation.

Log in to reply