簡體   English   中英

如何在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 +"\"");
}

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM