1. 程式人生 > >Qt之QDesktopServices 和 QStandardPaths

Qt之QDesktopServices 和 QStandardPaths

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等目錄的路徑 

QStandardPaths::standardLocations 獲得各種路徑

QCoreApplication::applicationDirPath() 獲得可執行檔案的路徑。