如何在 Qt/C++ 中仅将文件路径作为字符串访问文件
How to access a file with only the filepath as a string in Qt/C++?
我目前有我想以字符串形式访问的文件的文件路径,但我不确定Qt是否具有允许您仅使用文件路径访问该文件的功能。
我正在将文件路径保存到 INI 文件,我想采用该文件路径来打开作为该路径一部分的 json 文件。这就是我到目前为止尝试过的 - 代码将进入 openFile((
void saveFileLocation(QString filename)
{
QSettings *settings = new QSettings(Ve::Widgets::SettingsWindowWidget::INI_FILE, QSettings::IniFormat);
QDir dir;
QString filePath = dir.filePath(filename);
settings->setValue("projectFile", filePath);
on_menuRecent_Files_aboutToShow(filePath);
}
void openFile(QString filepath)
{
*insert code here*
}
void on_menuRecent_Files_aboutToShow(QString filePath)
{
QAction* openAction = ui->menuRecent_Files->addAction(filePath);
connect(openAction, SIGNAL(triggered()), this, SLOT(openFile(filePath)));
}
我想实现一个操作选项,其中包含文件路径的文本,单击时,将打开我要访问的文件。是否有Qt功能允许您执行此操作?
试试这个:
- 要使用系统默认关联应用程序打开:
void openFile(QString filepath)
{
QDesktopServices::openUrl(QUrl::fromLocalFile( filepath ));
}
- 要使用特定应用程序打开:
void openFile(QString filepath)
{
// open with notepad
QProcess::execute("notepad ""+ filepath +""");
}
希望对您有所帮助。
相关文章:
- 从函数角度看ID到文件路径的内部与外部映射
- 在C++中设置基于操作系统的文件路径
- Windows 非 ASCII 文件路径
- 非 ASCII 文件路径窗口
- C++和带有国家符号的文件路径(也许用 UTF8 编码)
- 如何为文件路径使用变量?
- 包括 VS Code 上的完整文件路径
- 如何使用提升获取当前文件路径?
- 如何在 c++ 中获取当前文件路径?
- 保存json文件后如何返回文件路径
- 从 C++ 中的文件路径中提取文件名
- 是否可以将文件路径传递给Qt中的setStyleSheet()
- 无法转换 .CATPart 文件.错误:输入文件路径似乎包含不支持的字符
- 在结构函数之间传递文件路径 C++ 编辑:修复LNK2019错误
- 提供依赖于输入的程序的文件路径
- 从 QFileSystemModel 中的文件路径和文件名获取 QModelIndex
- 如何更改变量文件名的文件路径?
- cmake:包括vs vs add_subDirectory:相对标头文件路径
- 在OSX中使用CGDisplayStream存储屏幕记录的特定文件路径
- 如何在另一个系统上启用文件路径