Qt 禁用无响应超时

Qt Disable Not Responding timeout?

本文关键字:响应超时 Qt      更新时间:2023-10-16

在我的项目中,我有很多循环进行许多计算,这可能需要相当长的时间。一切运行良好,直到我在加载时出现错误。当它计算所有内容时,我的程序变得"无响应",直到它完成加载。似乎有某种计时器,当它达到 60 秒时,调试停止,我收到此错误:

Exception at 0x7585b9bc, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) in Qt5Cored!QTimer::isSingleShot

有没有办法禁用该响应计时器?

感谢您抽出宝贵时间:)

编辑:这是循环。它非常抽象,所以我很抱歉你不知道这个循环的完整上下文。:/但要知道这一点,程序不会在同一时间点冻结。有时当 i== 498 时,有时当 499 时。等。

   for(int i=0; i<=728; i++)
    {
        int sections;
        if(PCBRchunks[i] != "Null")
        {
            QString blockId;
            PCBRblocks[i].fill('0', 131072);
            //Get number of sections
            temp_hex = PCBRchunks[i].mid(PCBRchunks[i].indexOf("09000853656374696f6e73")+30, 2);
            HexToInt2(temp_hex, temp_int);
            sections = temp_int;
            int totalStart;
            totalStart = PCBRchunks[i].indexOf("070006426c6f636b7300001000")+26;
            for(int s=0; s<=sections-1; s++)
            {
                int y;
                temp_hex = PCBRchunks[i].mid(totalStart + (s*20604) + 20574, 2);
                HexToInt2(temp_hex, y);
                for(int k=0; k<=255; k++)
                {
                    for(int p=0; p<=15; p++)
                    {
                        blockId = PCBRchunks[i].mid(totalStart + (s*20604) + (p*512) + ((((k-((k/16)*16))*16)+(k/16))*2), 2);
                        if(y<8)
                            PCBRblocks[i].replace((p*2)+(k*256)+(y*32), 2, blockId);
                        else
                            PCBRblocks[i].replace((p*2)+(k*256)+((y-8)*32)+65536, 2, blockId);
                    }
                }
            }
        }
        if(PCBLchunks[i] != "Null")
        {
            QString blockId;
            PCBLblocks[i].fill('0', 131072);
            //Get number of sections
            temp_hex = PCBLchunks[i].mid(PCBLchunks[i].indexOf("09000853656374696f6e73")+30, 2);
            HexToInt2(temp_hex, temp_int);
            sections = temp_int;
            int totalStart;
            totalStart = PCBLchunks[i].indexOf("070006426c6f636b7300001000")+26;
            for(int s=0; s<=sections-1; s++)
            {
                int y;
                temp_hex = PCBLchunks[i].mid(totalStart + (s*20604) + 20574, 2);
                HexToInt2(temp_hex, y);
                for(int k=0; k<=255; k++)
                {
                    for(int p=0; p<=15; p++)
                    {
                        blockId = PCBLchunks[i].mid(totalStart + (s*20604) + (p*512) + ((((k-((k/16)*16))*16)+(k/16))*2), 2);
                        if(y<8)
                            PCBLblocks[i].replace((p*2)+(k*256)+(y*32), 2, blockId);
                        else
                            PCBLblocks[i].replace((p*2)+(k*256)+((y-8)*32)+65536, 2, blockId);
                    }
                }
            }
        }
        if(PCTRchunks[i] != "Null")
        {
            QString blockId;
            PCTRblocks[i].fill('0', 131072);
            //Get number of sections
            temp_hex = PCTRchunks[i].mid(PCTRchunks[i].indexOf("09000853656374696f6e73")+30, 2);
            HexToInt2(temp_hex, temp_int);
            sections = temp_int;
            int totalStart;
            totalStart = PCTRchunks[i].indexOf("070006426c6f636b7300001000")+26;
            for(int s=0; s<=sections-1; s++)
            {
                int y;
                temp_hex = PCTRchunks[i].mid(totalStart + (s*20604) + 20574, 2);
                HexToInt2(temp_hex, y);
                for(int k=0; k<=255; k++)
                {
                    for(int p=0; p<=15; p++)
                    {
                        blockId = PCTRchunks[i].mid(totalStart + (s*20604) + (p*512) + ((((k-((k/16)*16))*16)+(k/16))*2), 2);
                        if(y<8)
                            PCTRblocks[i].replace((p*2)+(k*256)+(y*32), 2, blockId);
                        else
                            PCTRblocks[i].replace((p*2)+(k*256)+((y-8)*32)+65536, 2, blockId);
                    }
                }
            }
        }
        if(PCTLchunks[i] != "Null")
        {
            QString blockId;
            PCTLblocks[i].fill('0', 131072);
            //Get number of sections
            temp_hex = PCTLchunks[i].mid(PCTLchunks[i].indexOf("09000853656374696f6e73")+30, 2);
            HexToInt2(temp_hex, temp_int);
            sections = temp_int;
            int totalStart;
            totalStart = PCTLchunks[i].indexOf("070006426c6f636b7300001000")+26;
            for(int s=0; s<=sections-1; s++)
            {
                int y;
                temp_hex = PCTLchunks[i].mid(totalStart + (s*20604) + 20574, 2);
                HexToInt2(temp_hex, y);
                for(int k=0; k<=255; k++)
                {
                    for(int p=0; p<=15; p++)
                    {
                        blockId = PCTLchunks[i].mid(totalStart + (s*20604) + (p*512) + ((((k-((k/16)*16))*16)+(k/16))*2), 2);
                        if(y<8)
                            PCTLblocks[i].replace((p*2)+(k*256)+(y*32), 2, blockId);
                        else
                            PCTLblocks[i].replace((p*2)+(k*256)+((y-8)*32)+65536, 2, blockId);
                    }
                }
            }
        }
        qDebug() << i;
        ui->progressBar->setValue((i*100)/729);
    }

我认为它不在Qt中,而是在操作系统中。最佳解决方案是在工作线程中执行工作,以便 UI 线程可以同时处理消息。

通常的做法是为繁重的计算建立工作线程,以免使 UI 无响应。因此,工作线程将与主 UI 线程分离。

在这里,您可以找到一篇文章,其中包含非常完整的解释和示例,例如如何创建线程类,即使您可以将std::thread与 C++11 一起使用:

http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class

在这里,您可以找到一篇文章,其中包含有关如何创建工作线程的非常完整的解释和示例:

http://www.codeproject.com/Articles/552/Using-Worker-Threads

希望,它有帮助。