widget.cpp 7.1 KB

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