当前位置: 首页 > news >正文

利用Qt创建一个模拟问答系统

 

  1. 界面:
  • 添加了聊天显示区域(QTextEdit)
  • 添加了发送按钮和清空对话按钮
  • 优化了布局和窗口大小
  • 添加了时间戳显示

 2、功能:

  • 支持实时对话
  • 可以清空对话历史
  • 支持按回车发送消息
  • 添加了简单的关键词匹配响应系统
  1. 交互体验:
  • 消息自动滚动到底部
  • 清晰的用户/AI消息区分
  • 时间戳显示
  • 更友好的界面提示

目前的 getAIResponse 函数使用了一个简单的关键词匹配系统来模拟AI响应

具体代码:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QScrollArea>class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void handleSendMessage();void handleClearChat();private:void appendMessage(const QString &message, bool isUser);QString getAIResponse(const QString &userInput);QWidget *centralWidget;QVBoxLayout *mainLayout;QTextEdit *chatDisplay;QLineEdit *inputField;QPushButton *sendButton;QPushButton *clearButton;QScrollArea *scrollArea;
};#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include <QDateTime>
#include <QScrollBar>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{// 创建中心部件centralWidget = new QWidget(this);setCentralWidget(centralWidget);// 创建主布局mainLayout = new QVBoxLayout(centralWidget);// 创建聊天显示区域chatDisplay = new QTextEdit(this);chatDisplay->setReadOnly(true);chatDisplay->setStyleSheet("QTextEdit { background-color: #f5f5f5; }");mainLayout->addWidget(chatDisplay);// 创建输入区域QHBoxLayout *inputLayout = new QHBoxLayout();inputField = new QLineEdit(this);inputField->setPlaceholderText("输入你的问题...");inputLayout->addWidget(inputField);sendButton = new QPushButton("发送", this);inputLayout->addWidget(sendButton);clearButton = new QPushButton("清空对话", this);inputLayout->addWidget(clearButton);mainLayout->addLayout(inputLayout);// 连接信号和槽connect(sendButton, &QPushButton::clicked, this, &MainWindow::handleSendMessage);connect(clearButton, &QPushButton::clicked, this, &MainWindow::handleClearChat);connect(inputField, &QLineEdit::returnPressed, this, &MainWindow::handleSendMessage);// 设置窗口标题和大小setWindowTitle("AI 问答系统");resize(600, 400);// 添加欢迎消息appendMessage("你好!我是AI助手,有什么我可以帮你的吗?", false);
}MainWindow::~MainWindow()
{
}void MainWindow::handleSendMessage()
{QString userInput = inputField->text().trimmed();if (userInput.isEmpty()) {return;}// 显示用户消息appendMessage(userInput, true);inputField->clear();// 获取并显示AI响应QString aiResponse = getAIResponse(userInput);appendMessage(aiResponse, false);
}void MainWindow::handleClearChat()
{chatDisplay->clear();appendMessage("你好!我是AI助手,有什么我可以帮你的吗?", false);
}void MainWindow::appendMessage(const QString &message, bool isUser)
{QString timestamp = QDateTime::currentDateTime().toString("hh:mm:ss");QString formattedMessage = QString("[%1] %2: %3\n").arg(timestamp).arg(isUser ? "你" : "AI").arg(message);chatDisplay->append(formattedMessage);// 滚动到底部QScrollBar *scrollbar = chatDisplay->verticalScrollBar();scrollbar->setValue(scrollbar->maximum());
}QString MainWindow::getAIResponse(const QString &userInput)
{// 这里是一个简单的模拟响应QString response;if (userInput.contains("你好") || userInput.contains("hi") || userInput.contains("hello")) {response = "你好!很高兴见到你。";}else if (userInput.contains("名字")) {response = "我是AI助手,你可以叫我小智。";}else if (userInput.contains("天气")) {response = "抱歉,我目前无法获取实时天气信息。";}else if (userInput.contains("谢谢") || userInput.contains("感谢")) {response = "不客气!如果还有其他问题,随时问我。";}else if (userInput.contains("再见") || userInput.contains("拜拜")) {response = "再见!祝你有愉快的一天!";}else {response = "我理解你的问题,但可能需要更具体的描述。你能详细说明一下吗?";}return response;
}

 main.cpp

#include "mainwindow.h"#include <QApplication>
#include <QLocale>
#include <QTranslator>int main(int argc, char *argv[])
{QApplication a(argc, argv);// QTranslator translator;// const QStringList uiLanguages = QLocale::system().uiLanguages();// for (const QString &locale : uiLanguages) {//     const QString baseName = "QtDemo_" + QLocale(locale).name();//     if (translator.load(":/i18n/" + baseName)) {//         a.installTranslator(&translator);//         break;//     }// }MainWindow w;w.show();return a.exec();
}

 QtDemo.pro

QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.hFORMS += \mainwindow.uiTRANSLATIONS += \QtDemo_zh_CN.ts
CONFIG += lrelease
CONFIG += embed_translations# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 运行

 看不懂你的话可以先看第一个Qt项目

相关文章:

  • HCIE Datacom备考技巧
  • Kubernetes相关的名词解释POD(13)
  • Argo CD
  • 递归神经网络
  • 栈和队列--数据结构初阶(2)(C/C++)
  • 大数据系列 | 详解基于Zookeeper或ClickHouse Keeper的ClickHouse集群部署--完结
  • 查看MAC 地址以及简单了解
  • jvm-描述符与特征签名的区别
  • JavaScript-原型、原型链详解
  • Office文件内容提取 | 获取Word文件内容 |Javascript提取PDF文字内容 |PPT文档文字内容提取
  • 聊透多线程编程-线程互斥与同步-13. C# Mutex类实现线程互斥
  • 图片压缩工具,多种压缩方案可选
  • requestAnimationFrame是什么?【前端】
  • 基于瑞芯微RK3576国产ARM八核2.2GHz A72 工业评估板——ROS2系统使用说明
  • MH2103系列coremark1.0跑分数据和优化,及基于arm2d的优化应用
  • 鸿蒙NEXT开发LRUCache缓存工具类(单例模式)(ArkTs)
  • Gmssl实战
  • OpenSSL1.1.1d windows安装包资源使用
  • 【C++编程入门】:从零开始掌握基础语法
  • Python常用的第三方模块【openpyxl库】读写Excel文件
  • 诺奖得主等数十位经济学家发表宣言反对美关税政策
  • 新华社经济随笔:把握不确定性中的确定性
  • 第六季了,姐姐们还能掀起怎样的风浪
  • 梅德韦杰夫:如果欧盟和美国 “撒手不管”,俄罗斯会更快解决俄乌冲突
  • 一女子称醉酒后疑似被性侵,长沙警方通报:嫌犯邱某某已被刑拘
  • 1672万!大乐透8.8亿派奖第4期松江彩民18元中头奖