QT6 写一个诗词鉴赏、朗诵、阅读程序(智谱清言AI赏析接口)

实现了一个基于Qt框架的古诗词浏览应用。主要功能包括:通过PoemDatabase类管理诗词数据,首次运行时自动检查并导入Chinese-poetry目录中的数据;提供朝代筛选、关键词搜索和诗词浏览功能;双击诗词可查看详情和朗诵;界面采用qss样式表实现古风设计。核心类包括MainWindow、PoemDetailDialog和PoemPlayer,实现了数据管理、界面交互和语音朗诵功能。代码结构清晰,包含数据加载、UI初始化、样式应用和事件处理等模块。

使用网络上Chinese-poetry数据,github上有,gitee也有

以下是mainwindow.cpp

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "poemdetaildialog.h"
#include <QFile>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , poemDB(new PoemDatabase(this))
    , poemPlayer(new PoemPlayer(this))
{
    ui->setupUi(this);
    
    // 检查是否需要导入诗词数据
    if (poemDB->isDatabaseEmpty()) {
        // 导入数据(这里假设有一个默认的Chinese-poetry目录)
        QString poetryDir = QDir::currentPath() + "/Chinese-poetry";
        if (QDir(poetryDir).exists()) {
            QProgressDialog progressDialog("正在导入诗词数据...", "取消", 0, 100, this);
            progressDialog.setWindowModality(Qt::WindowModal);
            
            // 连接进度信号
            connect(poemDB, &PoemDatabase::importProgress, &progressDialog, [&progressDialog](int progress, const QString &message) {
                progressDialog.setValue(progress);
                progressDialog.setLabelText(message);
            });
            
            poemDB->importPoetryData(poetryDir);
        }
    }
    
    setupUI();
    applyStyleSheet();
    
    // 导入按钮暂时不在UI中,通过其他方式触发导入功能
}

MainWindow::~MainWindow()
{
    delete ui;
    delete poemDB;
    delete poemPlayer;
}

void MainWindow::setupUI()
{
    // 设置朝代下拉框
    setupDynastyComboBox();
    
    // 初始加载所有古诗
    loadPoemsList(poemDB->getAllPoems());
    
    // 设置列表项的提示
    ui->statusLabel->setText("双击诗句查看详情、赏析和朗诵");
    
    // 连接信号和槽
    connect(ui->searchButton, &QPushButton::clicked, this, &MainWindow::on_searchButton_clicked);
    connect(ui->dynastyComboBox, &QComboBox::currentTextChanged, this, &MainWindow::on_dynastyComboBox_currentIndexChanged);
    connect(ui->poemList, &QListWidget::itemDoubleClicked, this, &MainWindow::on_poemList_itemDoubleClicked);
    connect(ui->refreshButton, &QPushButton::clicked, this, &MainWindow::on_refreshButton_clicked);
}

void MainWindow::loadPoemsList(const QList<Poem> &poems)
{
    ui->poemList->clear();
    
    for (const Poem &poem : poems) {
        QString displayText = QString("《%1》 - %2 (%3)")
            .arg(poem.title)
            .arg(poem.author)
            .arg(poem.dynasty);
        
        QListWidgetItem *item = new QListWidgetItem(displayText, ui->poemList);
        item->setData(Qt::UserRole, poem.id);
        item->setToolTip(poem.content.left(100) + (poem.content.length() > 100 ? "..." : ""));
    }
    
    ui->statusLabel->setText(QString("共找到 %1 首诗词").arg(poems.size()));
}

void MainWindow::applyStyleSheet()
{
    QFile styleFile("qrc:/styles/ancient_style.qss");
    if (styleFile.open(QIODevice::ReadOnly)) {
        QString styleSheet = QLatin1String(styleFile.readAll());
        this->setStyleSheet(styleSheet);
        styleFile.close();
    } else {
        // 如果无法从资源加载,尝试从本地文件加载
        QFile localStyleFile("./ancient_style.qss");
        if (localStyleFile.open(QIODevice::ReadOnly)) {
            QString styleSheet = QLatin1String(localStyleFile.readAll());
            this->setStyleSheet(styleSheet);
            localStyleFile.close();
        }
    }
}

void MainWindow::setupDynastyComboBox()
{
    ui->dynastyComboBox->clear();
    ui->dynastyComboBox->addItem("所有朝代");
    
    // 从数据库获取所有朝代
    QStringList dynasties = poemDB->getAllDynasties();
    
    // 添加固定的朝代顺序(如果数据库中存在)
    QStringList orderedDynasties = {"先秦", "两汉", "魏晋南北朝", "唐代", "五代", "宋代", "元代", "明代", "清代"};
    QStringList resultDynasties;
    
    // 按固定顺序添加存在的朝代
    foreach (const QString &dynasty, orderedDynasties) {
        if (dynasties.contains(dynasty)) {
            resultDynasties << dynasty;
            dynasties.removeOne(dynasty);
        }
    }
    
    // 添加剩余的朝代
    resultDynasties << dynasties;
    
    ui->dynastyComboBox->addItems(resultDynasties);
}

// 导入功能暂时通过程序启动时自动检测实现
// 后续可以添加到菜单或工具栏中


// 朝代筛选功能已移至on_dynastyComboBox_currentIndexChanged方法中

void MainWindow::on_searchButton_clicked()
{
    QString keyword = ui->searchLineEdit->text().trimmed();
    if (keyword.isEmpty()) {
        // 如果搜索框为空,显示所有诗词
        if (ui->dynastyComboBox->currentText() == "所有朝代") {
            loadPoemsList(poemDB->getAllPoems());
        } else {
            loadPoemsList(poemDB->filterByDynasty(ui->dynastyComboBox->currentText()));
        }
    } else {
        // 执行搜索
        QList<Poem> results = poemDB->searchPoems(keyword);
        
        // 如果选择了特定朝代,进一步筛选
        if (ui->dynastyComboBox->currentText() != "所有朝代") {
            QList<Poem> filteredResults;
            for (const Poem &poem : results) {
                if (poem.dynasty == ui->dynastyComboBox->currentText()) {
                    filteredResults.append(poem);
                }
            }
            results = filteredResults;
        }
        
        loadPoemsList(results);
    }
}

void MainWindow::on_poemList_itemDoubleClicked(QListWidgetItem *item)
{
    int poemId = item->data(Qt::UserRole).toInt();
    Poem poem = poemDB->getPoemById(poemId);
    
    if (poem.id != 0) { // 确保找到了有效的诗词
        PoemDetailDialog *dialog = new PoemDetailDialog(poem, poemDB, poemPlayer, this);
        connect(dialog, &PoemDetailDialog::dialogClosed, this, &MainWindow::onPoemDetailClosed);
        dialog->setAttribute(Qt::WA_DeleteOnClose);
        dialog->show();
    }
}

void MainWindow::on_dynastyComboBox_currentIndexChanged(const QString &arg1)
{
    Q_UNUSED(arg1);
    on_searchButton_clicked(); // 朝代改变时重新搜索
}

void MainWindow::on_refreshButton_clicked()
{
    ui->searchLineEdit->clear();
    ui->dynastyComboBox->setCurrentIndex(0); // 重置为"所有朝代"
    loadPoemsList(poemDB->getAllPoems());
}

void MainWindow::onPoemDetailClosed()
{
    // 当诗歌详情对话框关闭时的处理逻辑
    // 这里可以添加清理或更新UI的代码
}

需要完整代码可以私信获取。

相关推荐
morn_venus2 小时前
使用qt控制台抓取tcp数据包
c++
小白跃升坊2 小时前
信息检索类智能体构建范式
人工智能·ai·全文检索·智能体
王哈哈^_^3 小时前
【完整源码+数据集】蓝莓数据集,yolo11蓝莓成熟度检测数据集 3023 张,蓝莓成熟度数据集,目标检测蓝莓识别算法系统实战教程
人工智能·算法·yolo·目标检测·计算机视觉·ai·视觉检测
apocelipes3 小时前
C++ Two Phase Lookup导致的模板代码编译错误
c++·泛型编程
hansang_IR3 小时前
【题解】洛谷 P1477 [NOI2008] 假面舞会 [思维 + 图论]
c++·算法·图论·思维
带刺的坐椅4 小时前
(对标 Spring IA 和 LangChain4j)Solon AI & MCP v3.7.0, v3.6.4, v3.5.8 发布(支持 LTS)
java·spring·ai·solon·mcp·langchain4j
卡提西亚4 小时前
一本通网站1125题:矩阵乘法
c++·算法·矩阵·编程题·一本通
郝学胜-神的一滴5 小时前
Linux的waitpid函数:深入解析与应用实践
linux·服务器·c++·程序人生
HyperAI超神经7 小时前
解决蛋白质构象异质性的原子级建模挑战!David Baker团队PLACER框架解析
人工智能·深度学习·ai·ai4s·蛋白质结构