test_image_chat.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <QApplication>
  2. #include <QWidget>
  3. #include <QVBoxLayout>
  4. #include <QHBoxLayout>
  5. #include <QPushButton>
  6. #include <QLineEdit>
  7. #include <QFileDialog>
  8. #include <QLabel>
  9. #include <QTimer>
  10. #include "chatview.h"
  11. class ImageChatTestWidget : public QWidget
  12. {
  13. Q_OBJECT
  14. public:
  15. ImageChatTestWidget(QWidget *parent = nullptr)
  16. : QWidget(parent)
  17. {
  18. setupUI();
  19. connectSignals();
  20. // 添加一些测试消息
  21. addTestMessages();
  22. }
  23. private slots:
  24. void sendTextMessage()
  25. {
  26. QString text = m_textEdit->text();
  27. if (!text.isEmpty()) {
  28. m_chatView->addMessage(text, ":/icons/user.png", "我", false);
  29. m_textEdit->clear();
  30. // 模拟回复
  31. QTimer::singleShot(1000, [this, text]() {
  32. m_chatView->addMessage("收到:" + text, ":/icons/bot.png", "助手", true);
  33. });
  34. }
  35. }
  36. void sendImageMessage()
  37. {
  38. QString imagePath = QFileDialog::getOpenFileName(this,
  39. "选择图片",
  40. "",
  41. "图片文件 (*.png *.jpg *.jpeg *.bmp *.gif)");
  42. if (!imagePath.isEmpty()) {
  43. QString text = m_textEdit->text();
  44. m_chatView->addImageMessage(imagePath, ":/icons/user.png", "我", false, text);
  45. m_textEdit->clear();
  46. // 模拟回复
  47. QTimer::singleShot(1000, [this]() {
  48. m_chatView->addMessage("收到了你的图片!", ":/icons/bot.png", "助手", true);
  49. });
  50. }
  51. }
  52. private:
  53. void setupUI()
  54. {
  55. setWindowTitle("图片聊天测试");
  56. resize(600, 800);
  57. QVBoxLayout *mainLayout = new QVBoxLayout(this);
  58. // 聊天视图
  59. m_chatView = new ChatView(this);
  60. mainLayout->addWidget(m_chatView);
  61. // 输入区域
  62. QHBoxLayout *inputLayout = new QHBoxLayout();
  63. m_textEdit = new QLineEdit(this);
  64. m_textEdit->setPlaceholderText("输入消息...");
  65. inputLayout->addWidget(m_textEdit);
  66. m_sendTextBtn = new QPushButton("发送文字", this);
  67. inputLayout->addWidget(m_sendTextBtn);
  68. m_sendImageBtn = new QPushButton("发送图片", this);
  69. inputLayout->addWidget(m_sendImageBtn);
  70. mainLayout->addLayout(inputLayout);
  71. }
  72. void connectSignals()
  73. {
  74. connect(m_sendTextBtn, &QPushButton::clicked, this, &ImageChatTestWidget::sendTextMessage);
  75. connect(m_sendImageBtn, &QPushButton::clicked, this, &ImageChatTestWidget::sendImageMessage);
  76. connect(m_textEdit, &QLineEdit::returnPressed, this, &ImageChatTestWidget::sendTextMessage);
  77. }
  78. void addTestMessages()
  79. {
  80. m_chatView->addSystemMessage("欢迎使用图片聊天功能测试!");
  81. m_chatView->addMessage("你好!这是一条普通的文本消息。", ":/icons/bot.png", "助手", true);
  82. m_chatView->addMessage("你好!我可以发送图片了吗?", ":/icons/user.png", "我", false);
  83. m_chatView->addMessage("当然可以!点击'发送图片'按钮选择图片文件。", ":/icons/bot.png", "助手", true);
  84. }
  85. private:
  86. ChatView *m_chatView;
  87. QLineEdit *m_textEdit;
  88. QPushButton *m_sendTextBtn;
  89. QPushButton *m_sendImageBtn;
  90. };
  91. // #include "test_image_chat.moc"
  92. // int main(int argc, char *argv[])
  93. // {
  94. // QApplication app(argc, argv);
  95. // ImageChatTestWidget widget;
  96. // widget.show();
  97. // return app.exec();
  98. // }