通过工具栏动作打开指定文件夹
以我自己的项目为例说明如何在QT中设置通过工具栏动作打开指定的文件,下图为我自己设计的ui界面,其中在拍照菜单下设计了打开文件夹的功能。
首先在mainwindow.h下声明这几个头文件
#include <QFile>
#include <QDesktopServices>
#include <QUrl>
#include <QDebug>
声明槽函数:void OpenFilePhoto();
在mainwindow.cpp中将打开文件的槽函数与信号函数关联起来:
connect(ui->actionopenfilephoto,SIGNAL(triggered()),this,SLOT(OpenFilePhoto()));
实现函数:
void MainWindow::OpenFilePhoto()
{
QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径。
QString hglpName = "photo";
QString hglpPath = QString("%1/%2").arg(runPath).arg(hglpName);
QFile bfilePath(hglpPath);
if(!bfilePath.exists()){
return;
}
QString filePath = "file:///" + hglpPath;
QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
}
其中photo文件夹要先在程序运行文件下声明。photo大小写都可以。
运行效果如下: