widget.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "widget.h"
  2. #include "./ui_widget.h"
  3. #include <QNetworkReply>
  4. #include <QNetworkAccessManager>
  5. #include <QNetworkRequest>
  6. #include <QTimer>
  7. #include <QNetworkProxy>
  8. #include <iostream>
  9. #include <regex>
  10. #include <stdlib.h>
  11. #include "QtFtp.h"
  12. #include "spdlog/spdlog.h"
  13. #include "fmtlog.h"
  14. #include "LightLog.h"
  15. #include "nlohmann/json.hpp"
  16. #define nJson nlohmann::json
  17. Widget::Widget(QWidget *parent)
  18. : QWidget(parent)
  19. , ui(new Ui::Widget)
  20. {
  21. ui->setupUi(this);
  22. m_curlFtp.setFtpIPAndPort("192.168.50.100", 21);
  23. m_curlFtp.setFtpUsernameAndPassword("microsoft", "19980714Lq");
  24. SPDLOG_INFO("***** Qt Library *****");
  25. }
  26. Widget::~Widget()
  27. {
  28. delete ui;
  29. }
  30. /* 解析JSON示例 */
  31. void Widget::parseJSON()
  32. {
  33. SPDLOG_INFO("解析JSON");
  34. nJson json0;
  35. json0["name"] = "microsoft";
  36. json0["age"] = 18;
  37. json0["bool"] = true;
  38. json0["null"] = nullptr;
  39. json0["array"] = {1, 2, 3, 4, 5};
  40. json0["object"] = {{"key1", "value1"}, {"key2", "value2"}};
  41. /* 打印json值 */
  42. SPDLOG_INFO("json0: {}", json0.dump());
  43. std::string name = json0["name"];
  44. std::string name2 = json0["name"].get<std::string>();
  45. std::string name1 = json0.at("name").get<std::string>();
  46. std::string name5 = json0.at("name");
  47. std::string name4 = json0.value("name", "default");
  48. int age = json0["age"].get<int>();
  49. bool b = json0["bool"];
  50. nJson null = json0["null"];
  51. nJson array = json0["array"];
  52. nJson object = json0["object"];
  53. /* 如果json中的值是null,在赋值给string时可能会报错,可以使用“?”运算符解决 */
  54. std::string name3 = json0["name"].is_null() ? "" : json0["name"].get<std::string>();
  55. /* 从直接到JSON */
  56. nJson json1 = R"(
  57. {
  58. "name": "microsoft",
  59. "age": 18,
  60. "bool": true,
  61. "null": null,
  62. "array": [1, 2, 3, 4, 5],
  63. "object": {
  64. "key1": "value1",
  65. "key2": "value2"
  66. }
  67. }
  68. )"_json;
  69. /* 或者 */
  70. std::string str = R"(
  71. {
  72. "name": "microsoft",
  73. "age": 18,
  74. "bool": true,
  75. "null": null,
  76. "array": [1, 2, 3, 4, 5],
  77. "object": {
  78. "key1": "value1",
  79. "key2": "value2"
  80. }
  81. }
  82. )";
  83. nJson json2 = nJson::parse(str);
  84. /* 从JSON到字符串 */
  85. std::string str1 = json0.dump();
  86. }
  87. void Widget::on_pBtn_connect_clicked()
  88. {
  89. SPDLOG_INFO("点击了“连接按钮”");
  90. std::vector<std::string> list;
  91. m_curlFtp.getFileList("/SSD2/Video", list);
  92. for(const std::string& filename : list)
  93. {
  94. QString qfilename = QString::fromLocal8Bit(filename.c_str());
  95. SPDLOG_INFO("{}", qfilename.toStdString());
  96. }
  97. }
  98. void Widget::on_pBtn_downloadFile_clicked()
  99. {
  100. SPDLOG_INFO("点击了“下载文件”");
  101. std::vector<std::string> fileList;
  102. bool ret = m_curlFtp.downloadFile("/SSD2/Video/哪吒之魔童降世.mp4", QApplication::applicationDirPath().toStdString() + "/YPM.mp4");
  103. if(ret)
  104. {
  105. SPDLOG_INFO("下载成功");
  106. }
  107. else
  108. {
  109. SPDLOG_INFO("下载失败");
  110. }
  111. }
  112. void Widget::on_pBtn_downloadVideo_clicked()
  113. {
  114. SPDLOG_INFO("点击了“下载视频”");
  115. // std::shared_ptr<QtFtp> ftp = std::make_shared<QtFtp>();
  116. // ftp->setHostAndPort("192.168.50.100");
  117. // ftp->setUserPasswd("microsoft", "19980714Lq");
  118. // QString localPath = QApplication::applicationDirPath() + "/v1.mp4";
  119. // ftp->getFile(localPath, "/SSD1/Video/v1.mp4");
  120. // ftp->waitFinished(-1);
  121. // if(ftp->getResult())
  122. // {
  123. // SPDLOG_INFO("下载成功");
  124. // }
  125. // else
  126. // {
  127. // SPDLOG_INFO("下载失败");
  128. // }
  129. bool ret = m_curlFtp.createDirectories("SSD1/DOC/123/234");
  130. if(ret)
  131. {
  132. SPDLOG_INFO("文件夹创建成功");
  133. }else {
  134. SPDLOG_WARN("文件夹创建失败");
  135. }
  136. }
  137. void Widget::on_pBtn_logSpeed_clicked()
  138. {
  139. SPDLOG_INFO("点击了“日志速度”");
  140. /* 开始时间点1 */
  141. std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
  142. for(int i = 0; i < 100000; i++)
  143. {
  144. SPDLOG_INFO("{}", "你好,这是日志速度的对比,这个是SPDLOG在打印日志");
  145. }
  146. /* 结束时间点1 */
  147. std::chrono::steady_clock::time_point spdlogEnd = std::chrono::steady_clock::now();
  148. for(int i = 0; i < 100000; i++)
  149. {
  150. FMTLOG_INFO("{}", "你好,这是日志速度的对比,这个是FMTLOG在打印日志");
  151. }
  152. std::chrono::steady_clock::time_point fmtlogEnd = std::chrono::steady_clock::now();
  153. /* 使用std::cout */
  154. for(int i = 0; i < 100000; i++)
  155. {
  156. std::cout << "你好,这是日志速度的对比," << "这个是std::cout在打印日志" << std::endl;
  157. }
  158. std::chrono::steady_clock::time_point coutEnd = std::chrono::steady_clock::now();
  159. /* 使用printf */
  160. for(int i = 0; i < 100000; i++)
  161. {
  162. printf("%s\n","你好,这是日志速度的对比,这个是printf在打印日志");
  163. }
  164. std::chrono::steady_clock::time_point printfEnd = std::chrono::steady_clock::now();
  165. /* 使用QDebug */
  166. for(int i = 0; i < 100000; i++)
  167. {
  168. qDebug() << "你好,这是日志速度的对比," << "这个是QDebug在打印日志";
  169. }
  170. std::chrono::steady_clock::time_point qDebugEnd = std::chrono::steady_clock::now();
  171. /* 使用LightLog */
  172. for(int i = 0; i < 100000; i++)
  173. {
  174. QLOG_INFO("你好,这是日志速度的对比," + "这个是LightLog在打印日志");
  175. }
  176. std::chrono::steady_clock::time_point lightLogEnd = std::chrono::steady_clock::now();
  177. /* 计算时间 */
  178. std::chrono::duration<double> spdlogTime = std::chrono::duration_cast<std::chrono::duration<double>>(spdlogEnd - start);
  179. std::chrono::duration<double> fmtlogTime = std::chrono::duration_cast<std::chrono::duration<double>>(fmtlogEnd - spdlogEnd);
  180. std::chrono::duration<double> coutTime = std::chrono::duration_cast<std::chrono::duration<double>>(coutEnd - fmtlogEnd);
  181. std::chrono::duration<double> printfTime = std::chrono::duration_cast<std::chrono::duration<double>>(printfEnd - coutEnd);
  182. std::chrono::duration<double> qDebugTime = std::chrono::duration_cast<std::chrono::duration<double>>(qDebugEnd - printfEnd);
  183. std::chrono::duration<double> lightLogTime = std::chrono::duration_cast<std::chrono::duration<double>>(lightLogEnd - qDebugEnd);
  184. /* 打印时间 */
  185. SPDLOG_INFO("SPDLOG打印耗时: {}", spdlogTime.count());
  186. SPDLOG_INFO("FMTLOG打印耗时: {}", fmtlogTime.count());
  187. SPDLOG_INFO("std::cout打印耗时: {}", coutTime.count());
  188. SPDLOG_INFO("printf打印耗时: {}", printfTime.count());
  189. SPDLOG_INFO("QDebug打印耗时: {}", qDebugTime.count());
  190. SPDLOG_INFO("LightLog打印耗时: {}", lightLogTime.count());
  191. }
  192. void Widget::on_pBtn_upload_clicked()
  193. {
  194. SPDLOG_INFO("点击了“上传”");
  195. bool ret = m_curlFtp.uploadFile(QApplication::applicationDirPath().toStdString() + "/YPM.mp4", "/SSD2/Video/YPM.mp4");
  196. if(ret)
  197. {
  198. SPDLOG_INFO("上传成功");
  199. }
  200. else
  201. {
  202. SPDLOG_INFO("上传失败");
  203. }
  204. }