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 PDFprocess->start(sumatraPath, arguments);// 等待进程启动if (!process->waitForStarted()) {qDebug() << "Failed to start Sumatra PDF.";return;}// 获取进程的PIDqint64 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.";
}