?
- 界面:
- 添加了聊天顯示區域(QTextEdit)
- 添加了發送按鈕和清空對話按鈕
- 優化了布局和窗口大小
- 添加了時間戳顯示
?2、功能:
- 支持實時對話
- 可以清空對話歷史
- 支持按回車發送消息
- 添加了簡單的關鍵詞匹配響應系統
- 交互體驗:
- 消息自動滾動到底部
- 清晰的用戶/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項目