mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
实现服务端的QT应用界面
This commit is contained in:
@@ -2,9 +2,54 @@
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QHBoxLayout>
|
||||
#include <QTableWidget>
|
||||
#include <QTimer>
|
||||
#include <QTextEdit>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
#include <spdlog/sinks/base_sink.h>
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
#include <QTextEdit>
|
||||
#include <mutex>
|
||||
|
||||
class QtTextSink : public QObject, public spdlog::sinks::base_sink<std::mutex>
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
// 构造函数接收一个 QTextEdit*,但用 QPointer 来包装它
|
||||
explicit QtTextSink(QTextEdit* textEdit, QObject* parent = nullptr)
|
||||
: QObject(parent), textEdit(textEdit) {
|
||||
}
|
||||
|
||||
signals:
|
||||
// 定义一个信号,用于在主线程中追加文本
|
||||
void appendText(const QString& message);
|
||||
|
||||
protected:
|
||||
// 重写 sink_it_ 方法,处理每条日志
|
||||
void sink_it_(const spdlog::details::log_msg& msg) override {
|
||||
if (textEdit.isNull()) {
|
||||
// 如果 QTextEdit 已经被删除,则忽略这条日志
|
||||
return;
|
||||
}
|
||||
spdlog::memory_buf_t formatted;
|
||||
formatter_->format(msg, formatted);
|
||||
QString logMessage = QString::fromStdString(fmt::to_string(formatted));
|
||||
// 发射信号,通过Qt的事件循环在主线程中安全地更新UI
|
||||
emit appendText(logMessage);
|
||||
}
|
||||
|
||||
void flush_() override {}
|
||||
|
||||
private:
|
||||
QPointer<QTextEdit> textEdit; // 关键:使用 QPointer
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
class LabelPair
|
||||
{
|
||||
public:
|
||||
@@ -34,6 +79,9 @@ public:
|
||||
QLabel value;
|
||||
};
|
||||
|
||||
|
||||
class MyWorkspace;
|
||||
|
||||
class MainApp : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -42,12 +90,19 @@ public:
|
||||
|
||||
void setMyLayout();
|
||||
|
||||
|
||||
private slots:
|
||||
void onTimer();
|
||||
|
||||
public:
|
||||
struct {
|
||||
std::shared_ptr<LabelPair> weburl {};
|
||||
std::shared_ptr<MyWorkspace> workspace;
|
||||
} ui;
|
||||
|
||||
struct {
|
||||
std::shared_ptr<QGridLayout> main;
|
||||
} layout;
|
||||
|
||||
std::shared_ptr<QTimer> timer;
|
||||
};
|
||||
Reference in New Issue
Block a user