-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogwindow.cpp
75 lines (63 loc) · 1.68 KB
/
logwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "logwindow.h"
#include "ui_logwindow.h"
#include <QTime>
#include <QCloseEvent>
#include <QFile>
LogWindow* LogWindow::m_instance = nullptr;
LogWindow::LogWindow(QWidget *parent) :
QWidget(parent, Qt::Dialog),
ui(new Ui::LogWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
text = ui->text;
setWindowTitle("日志窗口");
setGeometry(parent->x() + parent->width(), y(), 400, 450);
this->hide();
setFocusPolicy(Qt::NoFocus);
connect(ui->save, &QPushButton::clicked, this, &LogWindow::save);
connect(ui->clear, &QPushButton::clicked, this, &LogWindow::clear);
}
void LogWindow::clear()
{
ui->text->clear();
}
void LogWindow::save()
{
QFile f(QString("Log_%1.log").arg(QTime::currentTime().toString("hh_mm_ss")));
if (f.open(QFile::WriteOnly| QIODevice::Text)) {
i("保存文件");
f.write(buffer.toStdString().c_str());
buffer.clear();
}
f.close();
}
LogWindow::~LogWindow()
{
delete ui;
}
LogWindow *LogWindow::newInstance(QWidget *parent)
{
if (m_instance == nullptr)
m_instance = new LogWindow(parent);
return m_instance;
}
void LogWindow::i(QString text)
{
getInstance()->text->append(QTime::currentTime().toString("hh:mm:ss")+":"+text);
getInstance()->buffer.append(QTime::currentTime().toString("hh:mm:ss")+":"+text);
getInstance()->buffer.append("\n");
}
void LogWindow::closeEvent(QCloseEvent *e)
{
emit onCloseButtonClicked();
e->accept();
}
//void LogWindow::i(char* text)
//{
// getInstance()->text->append(QTime::currentTime().toString("hh:mm:ss")+":"+text);
//}
LogWindow *LogWindow::getInstance()
{
return m_instance;
}