云计算百科
云计算领域专业知识百科平台

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

 

  • 界面:
    • 添加了聊天显示区域(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_OBJECT

    public:
    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 gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    CONFIG += 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.0

    SOURCES += \\
    main.cpp \\
    mainwindow.cpp

    HEADERS += \\
    mainwindow.h

    FORMS += \\
    mainwindow.ui

    TRANSLATIONS += \\
    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项目

    赞(0)
    未经允许不得转载:网硕互联帮助中心 » 利用Qt创建一个模拟问答系统
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!