Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions src/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,22 @@ MainWidget::MainWidget(QWidget *parent) :

MainWidget::~MainWidget()
{
//程序即将结束,线程标志结束
m_isEndThread = 0;
// 等待线程结束
if (m_loadImagethread) {
m_loadImagethread->requestInterruption(); // 请求线程中断
if (!m_loadImagethread->wait(3000)) { // 等待线程结束,超时时间为3秒
qCWarning(dmOcr) << "OCR thread TIMEOUT";
m_loadImagethread->terminate(); // 如果线程没有结束,强制终止线程
m_loadImagethread->deleteLater(); // 删除线程对象
m_loadImagethread = nullptr;
}
// 正常结束的情况下delete操作在槽函数中执行
m_isEndThread.store(false);

QThread *thread = m_loadImagethread;
m_loadImagethread = nullptr;
if (!thread)
return;

thread->requestInterruption();
if (!thread->wait(3000)) {
qCWarning(dmOcr) << "OCR thread TIMEOUT";
thread->terminate();
// terminate() is asynchronous; the object must not be destroyed while
// the worker can still access MainWidget and the OCR engine.
thread->wait();
}
delete thread;
}

void MainWidget::setupUi(QWidget *Widget)
Expand Down Expand Up @@ -349,10 +352,6 @@ void MainWidget::setupConnect()
connect(this, &MainWidget::sigResult, this, [ = ](const OcrResult &result) {
applyOcrResult(result);
deleteLoadingUi();
if(m_needReRunRec) {
m_needReRunRec = false;
runRec(false);
}
});
}

Expand Down Expand Up @@ -495,8 +494,8 @@ void MainWidget::openImage(const QImage &img, const QString &name)

void MainWidget::runRec(bool needSetImage)
{
if(m_loadImagethread && m_loadImagethread->isRunning()) {
m_needReRunRec = true;
if (m_loadImagethread) {
m_needReRunRec.store(true);
return;
}

Expand All @@ -509,20 +508,26 @@ void MainWidget::runRec(bool needSetImage)
if(needSetImage) {
OCREngine::instance()->setImage(*m_currentImg);
}
if (!m_loadImagethread) {
m_loadImagethread = QThread::create([ = ]() {
const OcrResult result = OCREngine::instance()->getRecognitionResult();
//判断程序是否退出
if (1 == m_isEndThread) {
emit sigResult(result);
}
});
}
connect(m_loadImagethread, &QThread::finished, [this](){
m_loadImagethread->deleteLater();
m_loadImagethread = nullptr;
QThread *thread = QThread::create([this]() {
const OcrResult result = OCREngine::instance()->getRecognitionResult();
if (m_isEndThread.load()) {
emit sigResult(result);
}
});
m_loadImagethread->start();
m_loadImagethread = thread;

// Always marshal cleanup back to MainWidget's thread. The previous
// context-less connection executed in the OCR thread and could access a
// null or dangling m_loadImagethread while the widget was being destroyed.
connect(thread, &QThread::finished, this, [this, thread]() {
if (m_loadImagethread == thread)
m_loadImagethread = nullptr;
thread->deleteLater();

if (m_needReRunRec.exchange(false))
runRec(false);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · New image shows stale OCR result

When a different image is opened in the same widget while OCR is still running, runRec(true) returns before OCREngine::setImage() and records only a boolean rerun request; the finished callback calls runRec(false), so the engine recognizes its previous image and displays that result for the new image.

Have the finished callback rerun OCR in a way that sets the engine's image to the newly opened image before recognition.

Also at src/mainwidget.cpp:497-499.

Prompt for AI agents
In `src/mainwidget.cpp` at line 528:

**New image shows stale OCR result**

When a different image is opened in the same widget while OCR is still running, `runRec(true)` returns before `OCREngine::setImage()` and records only a boolean rerun request; the finished callback calls `runRec(false)`, so the engine recognizes its previous image and displays that result for the new image.

Have the finished callback rerun OCR in a way that sets the engine's image to the newly opened image before recognition.

Also at `src/mainwidget.cpp:497-499`.

}, Qt::QueuedConnection);
thread->start();
}

void MainWidget::loadHtml(const QString &html)
Expand Down
4 changes: 3 additions & 1 deletion src/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
#include <DFrame>
#include <DComboBox>

#include <QMutex>

Check warning on line 17 in src/mainwidget.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QMutex> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DToolButton>

Check warning on line 18 in src/mainwidget.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DToolButton> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <atomic>

Check warning on line 20 in src/mainwidget.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <atomic> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "resulttextview.h"

#include "textloadwidget.h"
Expand Down Expand Up @@ -113,7 +115,7 @@

QWidget *m_emptyWidget;

int m_isEndThread = 1;
std::atomic_bool m_isEndThread{true};
QSettings *ocrSetting;
std::atomic_bool m_needReRunRec = false;

Expand Down
Loading