Search code examples
c++qtshellexecute

Open file with Standard editor and jump to a specific line


I'm programming a little tool to handle XML files. It is written in C++ with Qt.

To open such files with an Editor I used:

QDesktopServices::openUrl(QUrl::fromLocalFile(file.xml))

The next step was to open that XML file and jump to a specific line. I tried to use ShellExecute:

ShellExecute (NULL, "open", "Notepad.exe", "path.xml -l 200", NULL, SW_SHOWNORMAL);

However, it didn't work. The line above opens the file path.xml, but not at line 200. I tried the same with VIM instead of Notepad, with the same result. Where is my error?

Thanks for your help.


Solution

  • I would do it in the following way (Windows):

    QProcess proc;
    proc.startDetached("C:\\Program Files (x86)\\Notepad++\\Notepad++",
                        QStringList() << "file.xml" << "-n 2000");
    

    The code above opens an instance of Notepad++ application, loads file.xml file and scrolls to the line 2000 (-n command line switch) of the opened file.