如何从Qt启动外部文件

How to lauch an external file from Qt?

本文关键字:外部 文件 启动 Qt      更新时间:2023-10-16

我想打开一个pdf文件,只需双击QListWidget的一个元素。我创建了一个批处理文件来打开Acrobat Reader(Reader.bat),但我想要一个特定的pdf文件:

void MainWindow::on_FileListWidget_itemDoubleClicked(QListWidgetItem *item)
{
        QFile SelectedModel(Current_Path  + "/Template/" + item->text());
        QString FileName;
        FileName = (Current_Path  + "/Template/" + item->text());
        ::system("e:\reader.bat");
}

1) 我不知道从QFile或QString中可以更好地识别和选择QListWidget中的文件;2) 我不知道如何将文件添加到打开Acorbat阅读器的命令中(在显示的行中,我可以打开程序,但不能打开我的文件)。知道吗?

您可以使用Qt本机方法通过调用来实现此行为

QDesktopServices::openUrl(QUrl::fromLocalFile("someFilePath"));

请参阅QUrl::fromLocalFile()和QDesktopServices::openUrl()

(请参阅本文)

看看这个:http://qt-project.org/doc/qt-5/qdesktopservices.html#openUrl

QDesktopServices::openUrl(QUrl("file://path/to/file"));

这不仅可以用默认应用程序打开文件(如果使用file://方案),还可以用默认浏览器打开web URL(如果使用了http://https://方案),或者在使用mailto://方案的情况下用默认电子邮件客户端打开。它也适用于steam://itunes://等其他方案。