QT QDesktopServices 通用桌面服务的方法(运行打开文件)

QDesktopServices类提供了访问通用桌面服务的方法。

在适当的web浏览器为用户的桌面环境打开给定的URL,如果成功返回true,否则返回false。

如果URL是本地文件的一个引用,那么它会在一个合适的应用程序中打开,而不是在web浏览器中打开。

如果应用程序关闭,则之前打开的QUrl会自动关闭。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopServices>
#include <QUrl>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
 
    //打开一个网址
    QDesktopServices::openUrl(QUrl("http://www.baidu.com"));
    //打开一个文本文件
    QDesktopServices::openUrl(QUrl::fromLocalFile("e:/log.txt"));
    //打开一个应用程序
    QDesktopServices::openUrl(QUrl::fromLocalFile("C:/Program Files (x86)/NSIS/NSIS.exe"));
    //打开一个发送邮件
    QDesktopServices::openUrl(QUrl("mailto:[email protected]?subject=Test&body=Just a test"));
    //如果应用程序关闭,则之前打开的QUrl会自动关闭
}
 
MainWindow::~MainWindow()
{
    delete ui;
}

发布了18 篇原创文章 · 获赞 1 · 访问量 2179

猜你喜欢

转载自blog.csdn.net/leng3667/article/details/102964261