Qt使用绿色pdf阅读器打开文件

1.下载SumatraPDF

2.设置

 3.代码

void MainWindow::on_pushButton_clicked()
{
    QProcess *process = new QProcess();
    QString filePath = "C:\\Users\\jude\\Desktop\\su\\11.pdf";
    QString sumatraPath = "C:\\Users\\jude\\Desktop\\su\\SumatraPDF-3.5.2-64.exe"; // 请根据实际安装路径修改
    // 设置启动的程序和参数
    QStringList arguments;
    arguments << filePath;

    // 启动Sumatra PDF
    process->start(sumatraPath, arguments);

    // 等待进程启动
    if (!process->waitForStarted()) {
        qDebug() << "Failed to start Sumatra PDF.";
        return;
    }

    // 获取进程的PID
    qint64 pid = process->processId();
    qDebug() << "Sumatra PDF started with PID:" << pid;

    QTimer::singleShot(5000, [process]() {
        qDebug() << "Terminating Sumatra PDF...";
        process->terminate(); // 尝试优雅地终止进程
        if (!process->waitForFinished(3000)) { // 等待3秒
            qDebug() << "Sumatra PDF did not terminate gracefully. Killing it...";
            process->kill(); // 强制终止进程
            process->waitForFinished(); // 等待进程结束

            delete process;
        }
        qDebug() << "Sumatra PDF closed.";
    });

    qDebug() << "Sumatra PDF finished.";
}

 

posted @ 2024-09-10 23:47  朱小勇  阅读(16)  评论(0编辑  收藏  举报