#ifndef DETECTLOGHELPER_H #define DETECTLOGHELPER_H #include #include #include #include /** * @brief 检测日志辅助类 * * 用于管理现有 QListView 的日志显示逻辑,支持: * - 日志去重(相同消息显示计数,如 "x2", "x3") * - 自动添加时间戳 * - 自动滚动到底部 * - 跨线程安全更新 * * 使用方式: * 1. 在 mainwindow.h 中声明: DetectLogHelper* m_logHelper; * 2. 在构造函数中初始化: m_logHelper = new DetectLogHelper(ui->detect_log, this); * 3. 添加日志: m_logHelper->appendLog("消息"); * 4. 清空日志: m_logHelper->clearLog(); */ class DetectLogHelper : public QObject { Q_OBJECT public: /** * @brief 构造函数 * @param listView 要管理的 QListView 指针(通常是 ui->detect_log) * @param parent 父对象 */ explicit DetectLogHelper(QListView* listView, QObject *parent = nullptr); ~DetectLogHelper(); /** * @brief 添加日志消息(线程安全,可从任意线程调用) * @param message 日志消息内容 */ void appendLog(const QString& message); /** * @brief 清空所有日志(线程安全,可从任意线程调用) */ void clearLog(); /** * @brief 设置日期格式 * @param format 日期格式字符串,默认 "hh:mm:ss" */ void setTimestampFormat(const QString& format); /** * @brief 设置是否显示时间戳 * @param show 是否显示,默认 true */ void setShowTimestamp(bool show); /** * @brief 设置是否启用日志去重 * @param enable 是否启用,默认 true */ void setDeduplicationEnabled(bool enable); /** * @brief 获取内部 QStringListModel 指针 * @return QStringListModel 指针 */ QStringListModel* model() const; signals: /** * @brief 日志更新请求信号(内部使用,用于跨线程更新) */ void logUpdateRequested(const QString& message); /** * @brief 日志清空请求信号(内部使用,用于跨线程更新) */ void logClearRequested(); private slots: /** * @brief 在UI线程中更新日志 */ void updateLogInUI(const QString& message); /** * @brief 在UI线程中清空日志 */ void clearLogInUI(); private: QListView* m_listView; QStringListModel* m_logModel; // 日志去重相关 QString m_lastLogMessage; // 最后一条日志消息(不含时间戳) int m_lastLogCount; // 最后一条日志的重复次数 // 配置项 QString m_timestampFormat; // 时间戳格式 bool m_showTimestamp; // 是否显示时间戳 bool m_deduplicationEnabled; // 是否启用去重 // 初始化信号槽连接 void initConnections(); }; #endif // DETECTLOGHELPER_H