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的代码
}

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

相关推荐
mobility9 小时前
免费AI视频生成器:我如何用零成本做出带旁白字幕的多场景AI视频
ai·vibe coding
doiito13 小时前
【Agent Harness】Gliding Horse 给 Agent OS 装上双曲空间引擎与默克尔树边云同步
ai·rust·架构设计·系统设计·ai agent
knqiufan15 小时前
从 Python 到 TypeScript,用 GLM-5.2 跑通 PowerMem SDK 的长程任务工程
ai·memory·agentic·powermem
小白跃升坊1 天前
Codex 增强部署:基于 Codex++ 接入 DeepSeek
ai·ai编程·codex·deepseek·ai coding·codex++
AlfredZhao1 天前
GPT 省钱,不是别用最新模型,而是别浪费缓存
gpt·ai
doiito2 天前
【Agent Harness】Gliding Horse 本体论系统设计:给 AI Agent 装上“语义大脑”
ai·rust·架构设计·系统设计·ai agent
博客18002 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴2 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
小七-七牛开发者2 天前
周一上线 | SpaceX 收购 Cursor、支付宝进入 AI 时代、DeepSeek 完成 500 亿元融资
ai·agent·token·glm·智谱·claudecode·ai coding·周一上线
众少成多积小致巨3 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++