mainwindow.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "mainwindow.h"
  2. #include "customevent.h"
  3. #include <QApplication>
  4. #include <QDateTime>
  5. #include <QBoxLayout>
  6. #include "TcpServerPage.h"
  7. #include "UdpServerPage.h"
  8. #include "HttpServerPage.h"
  9. SINGLETON_IMPL(MainWindow)
  10. MainWindow::MainWindow(QWidget *parent)
  11. : QMainWindow(parent)
  12. {
  13. initUI();
  14. initConnect();
  15. }
  16. MainWindow::~MainWindow()
  17. {
  18. }
  19. void MainWindow::initUI()
  20. {
  21. initMenu();
  22. setFixedSize(800, 600);
  23. QVBoxLayout* vbox = new QVBoxLayout();
  24. tab = new QTabWidget;
  25. tab->addTab(new TcpServerPage, "TCP");
  26. tab->addTab(new UdpServerPage, "UDP");
  27. tab->addTab(new HttpServerPage, "HTTP | WebSocket");
  28. vbox->addWidget(tab);
  29. recvmsg = new QTextEdit();
  30. recvmsg->setReadOnly(true);
  31. vbox->addWidget(recvmsg);
  32. center = new QWidget;
  33. center->setLayout(vbox);
  34. setCentralWidget(center);
  35. }
  36. void MainWindow::initMenu()
  37. {
  38. }
  39. void MainWindow::initConnect()
  40. {
  41. }
  42. void MainWindow::postMessage(const QString &msg)
  43. {
  44. QStringEvent* event = new QStringEvent(msg);
  45. QApplication::postEvent(this, event);
  46. }
  47. void MainWindow::appendMessage(const QString& msg)
  48. {
  49. QString text = recvmsg->toPlainText();
  50. text += QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss.zzz] ");
  51. text += msg;
  52. if (text.back() != '\n') {
  53. text += "\n";
  54. }
  55. showMessage(text);
  56. }
  57. void MainWindow::showMessage(const QString& msg)
  58. {
  59. recvmsg->setText(msg);
  60. QTextCursor cursor = recvmsg->textCursor();
  61. cursor.movePosition(QTextCursor::End);
  62. recvmsg->setTextCursor(cursor);
  63. recvmsg->repaint();
  64. }
  65. void MainWindow::customEvent(QEvent* e)
  66. {
  67. switch(e->type())
  68. {
  69. case qEventRecvMsg:
  70. {
  71. QStringEvent* event = dynamic_cast<QStringEvent*>(e);
  72. appendMessage(event->message);
  73. }
  74. e->accept();
  75. break;
  76. default:
  77. break;
  78. }
  79. }