QT开发中文网

#include "videoplayer.h" #include "ui_videoplayer.h" videoplayer::videoplayer(QWidget *parent) : QMainWindow(parent) , ui(new Ui::videoplayer) { ui->setupUi(this); player=new QMediaPlayer(this); player->

以下是修改后的代码,请替换掉原来的on_pushButton_history_clicked()函数:

void videoplayer::on_pushButton_history_clicked() {

QString recordPath = "/path/to/record.txt"; // 历史记录文件路径

// 判断历史记录文件是否存在,不存在则创建一个新的文件
if (!QFile::exists(recordPath)) {
    QFile file(recordPath);
    if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        file.close();
    }
}

// 读取历史记录
QStringList records;
QFile file(recordPath);
if (file.open(QIODevice::ReadOnly)) {
    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
        QString imagePath = line.trimmed();
        records << imagePath;
    }
    file.close();
}

// 打开编辑窗口
bool ok;
QString newName = QInputDialog::getText(this, "编辑历史记录",
                                        "请输入新名称:", QLineEdit::Normal,
                                        records[0], &ok);
if (ok && !newName.isEmpty()) {
    records[0] = newName;
    // 将修改后的历史记录写入文件
    QFile file(recordPath);
    if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        QTextStream out(&file);
        for (QString& record : records) {
            out << record << "\n";
        }
        file.close();
    }

    // 初始化QTableWidget窗口
    m_tableWidget = new QTableWidget(records.size(), 1, this);
    m_tableWidget->setWindowTitle("历史记录");
    m_tableWidget->setHorizontalHeaderLabels(QStringList() << "路径");

    // 添加数据到表格中
    for (int i = 0; i < records.size(); i++) {
        QTableWidgetItem* item = new QTableWidgetItem(records[i]);
        m_tableWidget->setItem(i, 0, item);
    }
    m_tableWidget->setVerticalHeaderLabels(QStringList());
    m_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    m_tableWidget->setColumnWidth(0, 400);

    // 显示窗口
    m_tableWidget->show();
}

}

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=1352

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?