1
0
forked from BRT/arc
arc/main.cpp
trnsmkot 9fb9cf0005 Refactor UI and database logic for device management
Replaced `MyWindow` with `MainWindow` featuring dynamic device widgets. Introduced `DeviceInfo`, `DeviceDAO`, and improved database interaction. Added flow layout and refactored image handling for better performance.
2025-04-18 16:39:13 +07:00

79 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <QApplication>
#include <QThread>
#include "views/MainWindow.h"
#include "database/DatabaseManager.h"
#include "database/dao/CommonDataDAO.h"
#include "services/DeviceScreener.h"
void setupWorkerAndThread(QCoreApplication& app) {
auto* thread = new QThread;
auto* deviceScreenerWorker = new DeviceScreener;
// Перемещаем worker в новый поток thread.
// Это значит, что все слоты worker, вызываемые через connect(), теперь будут исполняться в этом фоне-потоке.
deviceScreenerWorker->moveToThread(thread);
// Когда поток запустится, автоматически вызовется worker->start() внутри фонового потока.
QObject::connect(thread, &QThread::started, deviceScreenerWorker, &DeviceScreener::start);
// 🔁 Эти 3 строки делают автоматическую зачистку:
// Когда worker закончит работу (emit finished()), мы:
// - останавливаем поток (thread->quit())
// - удаляем worker (deleteLater)
// - удаляем thread (deleteLater)
// Это защищает от утечек памяти и оставшихся потоков после завершения.
QObject::connect(deviceScreenerWorker, &DeviceScreener::finished, thread, &QThread::quit);
QObject::connect(deviceScreenerWorker, &DeviceScreener::finished, deviceScreenerWorker, &QObject::deleteLater);
QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);
// Когда пользователь закрывает приложение (Ctrl+C, exit() и т.п.), мы вызываем worker->stop() — ты сам пишешь, что в этом методе.
//Обычно он просто ставит флаг m_running = false, чтобы остановить бесконечный цикл в start()
QObject::connect(&app, &QCoreApplication::aboutToQuit, [=]() {
deviceScreenerWorker->stop();
});
thread->start();
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// Setup worker and thread
setupWorkerAndThread(app);
const QDateTime current = QDateTime::currentDateTimeUtc();
qDebug() << "Обновление lastScan:" << CommonDataDAO::updateLastScan(current);
const QDateTime savedTime = CommonDataDAO::getLastScan();
qDebug() << "Сохранившееся время:" << savedTime.toString();
//
// // tesseract, tesseract-lang
// QImage image("./images/img.png");
// if (image.isNull()) {
// qDebug() << "Cant load the image";
// return -1;
// }
//
// image = image.convertToFormat(QImage::Format_Grayscale8);
//
// tesseract::TessBaseAPI tess;
// if (tess.Init(nullptr, "rus")) {
// qDebug() << "Ошибка инициализации Tesseract с русским языком....";
// return -1;
// }
//
// tess.SetImage(image.bits(), image.width(), image.height(), 1, image.bytesPerLine());
//
// QString result = QString::fromUtf8(tess.GetUTF8Text());
// qDebug() << "Распознанный текст:" << result;
MainWindow window;
window.show();
return app.exec();
}