title: Printing a PDF file from Qt
body: <p>I have found following code snippet which works as my expectation, but the problem is that, when a PDF file is open and user print the PDF file with some other printer from the PDF reader and not close the PDF reader and again print the PDF file from my application, it will print with printer which is configured on PDF reader not the printer which I set as to print the PDF file.</p>
  
  <p>Example:</p>
  
  <ol>
  <li>On application I set Printer_1 as to print PDF file.</li>
  <li>Print the file (it will open the file in PDF reader and print with Printer_1 printer).</li>
  <li>On PDF reader I go to <code>CTRL + P</code> to print and there I select Printer_2 and click on print.</li>
  <li>I don't close the PDF reader and again on application (printer is selected to Printer_1), I print the PDF file. Now the print command is sent to Printer_2 instead of Printer_1.</li>
  </ol>
  
  <p>Please what is the problem in the following code snippet?</p>
  
  <pre><code>#include &lt;QSettings&gt;
  #include &lt;QProcess&gt;
  #include &lt;QDebug&gt;
  
  int main(int argc, char *argv[])
  {
      const QString classesRoot = "HKEY_CLASSES_ROOT";
  
      // get ID of .pdf extension
      QSettings pdfSettings(classesRoot + "\\.pdf", QSettings::NativeFormat);
      QString pdfId = pdfSettings.value("Default").toString();
  
      // get path to default program that associated with PDF files
      QString printPath = QSettings(classesRoot + "\\" + pdfId + "\\shell\\print\\command", QSettings::NativeFormat).value("Default").toString();
      QString openPath = QSettings(classesRoot + "\\" + pdfId + "\\shell\\open\\command", QSettings::NativeFormat).value("Default").toString();
      qDebug() &lt;&lt; "print path" &lt;&lt; printPath;
      qDebug() &lt;&lt; "open path" &lt;&lt; openPath;
  
      // open .pdf file
      QProcess::startDetached(openPath.arg("full path to pdf file.pdf") );
  
      // print .pdf file
      QProcess printProcess;
      printProcess.start(printPath.arg("full path to pdf file.pdf") );
      printProcess.waitForFinished(-1);
  
      return 0;
  }
  </code></pre>