title: Qt: How to open file by external programm, "open with..." dialog
body: <p>How can I open a file by associated, external program and if it fails call a "open with..." dialog? </p>
  
  <p>Can it be platform independent code? Or I need to use </p>
  
  <pre><code>#ifdef
  #else 
  #endif 
  </code></pre>
  
  <p>constructions to implement "open with..." dialog call for every platform? </p>
  
  <p>Thanks in advance</p>
  
  <p>EDIT: Update, after few hours of researching, i found good solution for Windows.</p>
  
  <p>We try to open file with ShellExecute(...), if it fails with file association error, we open file by "open as" Shell32.dll dialog</p>
  
  <pre><code>QString file_path = "C:\VeryGoodPath.txt";
  int iResult = (int)ShellExecuteA(0, 0, file_path.toStdString().c_str(), 0, 0 , SW_SHOW );
  if(iResult&gt;32)
  {
      // Succesful run
  }
  else
  {
      if(SE_ERR_ASSOCINCOMPLETE==iResult)
      {
          QString ShellCmd = "C:\\Windows\\system32\\shell32.dll,OpenAs_RunDLL \""+file_path +"\"";
          ShellExecuteA(0,"open", "C:\\Windows\\system32\\rundll32.exe",ShellCmd.toStdString().c_str(),NULL, SW_NORMAL);
      }
      else
      {
          // Errors
      }
  }
  </code></pre>
  
  <p>EDIT2: Problem was, that i dont use in</p>
  
  <pre><code> QDesktopServices::openUrl(...)
  </code></pre>
  
  <p>this function</p>
  
  <pre><code> QUrl::fromLocalFile("&lt;path_to_your_file&gt;")
  </code></pre>