如果使用Qt开发界面,往往离不开QDesktopServices,QDesktopServices不仅可以打开本地浏览器,而且还可以打开本地文件(夹)等,可以获取桌面、我的文档、Home等目录。。。好吧,实现比较简单!
1、打开浏览器网页
QUrl url(QString("www.google.com"));
bool is_open = QDesktopServices::openUrl(url);
2、打开本地文件(夹)、可执行程序等
QString local_path = QString("E:/新建文件夹"); //a.txt、a.exe、a.mp3、a.mp4、a.rmvb等
QString path = QString("file:///") + local_path;
bool is_open = QDesktopServices::openUrl(QUrl(path, QUrl::TolerantMode));
注意:这里local_path可以是文件(夹)路径、可执行程序路径,当为文件时,会选择默认打开方式进行打开!
3、获取桌面、我的文档、Home等目录的路径
QString desktop_path = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
QString document_path = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
QString home_path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
QString application_path = QDesktopServices::storageLocatio(QDesktopServices::ApplicationsLocation);
QString temp_path = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
在这里再罗嗦一点,QProcess也可以打开文件(夹)、可执行程序等
QString local_path = QString("E:\\新建文件夹");
bool is_start = QProcess::startDetached("explorer " + local_path);
Qt --- QDesktopServices 打开文件、目录、url
最新推荐文章于 2024-05-15 15:26:17 发布