0

I currently have the filepath of the file that I want to access as a string, but I'm not sure if Qt has a feature that allows you to access that file with only the filepath.

I'm saving the filepath to an INI file and I want to take that filepath to open the json file that is a part of that path. This is what I've tried so far - the code would go into 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)));


}

I want to implement an action option, that has text of the filepath, and when clicked, opens up the file that I want to access. Is there a Qt feature that allows you to do this?

8
  • 2
    "opens the file" as in "open the file to read its bytes" or "launch the associated application"? Commented Jun 7, 2019 at 20:39
  • saveFileLocation() is leaking the settings object. You don't really need to use new at all in that code, but if you do use it, you need to delete what you new. Commented Jun 7, 2019 at 20:41
  • What you mean by access is very vague. With that said Qt supports both options that @MatteoItalia mentioned. Commented Jun 7, 2019 at 20:42
  • I mean "launch the associated application" Commented Jun 7, 2019 at 20:43
  • 2
    doc.qt.io/qt-5/qdesktopservices.html#openUrl Commented Jun 7, 2019 at 20:44

1 Answer 1

1

Try this:

  • To open with system default associated application:
void openFile(QString filepath) 
{   
    QDesktopServices::openUrl(QUrl::fromLocalFile( filepath ));
}
  • To open with specific application:
void openFile(QString filepath) 
{   
    // open with notepad
    QProcess::execute("notepad \""+ filepath +"\"");
}

Hope it helps you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.