Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to select file and then print to printer ?
QtWS25 Last Chance

how to select file and then print to printer ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
printpdffileprintsupport
11 Posts 4 Posters 8.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qjay
    wrote on 8 Feb 2018, 15:51 last edited by
    #1

    Hello , i want to select file ( most probably PDF ) and then want to print it on paper.

    So far i have tried this

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QPrinter>
    #include <QPrintDialog>
    #include <QFile>
    #include <QFileDialog>
    #include <QDebug>
    #include <QMessageBox>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_select_file_clicked()
    {
        file_path = QFileDialog::getOpenFileName(this,"Select File","c://");
        qDebug()<<file_path;
    }
    
    void MainWindow::on_print_clicked()
    {
        QPrinter printer;
        QPrintDialog print_dialog(&printer,this);
        if(print_dialog.exec() == QDialog::Rejected)
        {
            QMessageBox msg;
            msg.setText("ERROR");
            msg.show();
        }
    
    
    
    
    
    }
    
    
    J 1 Reply Last reply 8 Feb 2018, 16:27
    0
    • Q Qjay
      8 Feb 2018, 15:51

      Hello , i want to select file ( most probably PDF ) and then want to print it on paper.

      So far i have tried this

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QPrinter>
      #include <QPrintDialog>
      #include <QFile>
      #include <QFileDialog>
      #include <QDebug>
      #include <QMessageBox>
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      
      void MainWindow::on_select_file_clicked()
      {
          file_path = QFileDialog::getOpenFileName(this,"Select File","c://");
          qDebug()<<file_path;
      }
      
      void MainWindow::on_print_clicked()
      {
          QPrinter printer;
          QPrintDialog print_dialog(&printer,this);
          if(print_dialog.exec() == QDialog::Rejected)
          {
              QMessageBox msg;
              msg.setText("ERROR");
              msg.show();
          }
      
      
      
      
      
      }
      
      
      J Offline
      J Offline
      JonB
      wrote on 8 Feb 2018, 16:27 last edited by
      #2

      @Qjay And what actually happens?

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qjay
        wrote on 9 Feb 2018, 05:12 last edited by
        #3

        hey when i select PDF file i get this in console

        QPrintDialog: Cannot be used on non-native printers
        

        but when i select other file like txt file or image file , i get prindialog like this
        0_1518153127756_Capture.PNG

        J 1 Reply Last reply 9 Feb 2018, 11:00
        0
        • Q Qjay
          9 Feb 2018, 05:12

          hey when i select PDF file i get this in console

          QPrintDialog: Cannot be used on non-native printers
          

          but when i select other file like txt file or image file , i get prindialog like this
          0_1518153127756_Capture.PNG

          J Offline
          J Offline
          JonB
          wrote on 9 Feb 2018, 11:00 last edited by
          #4

          @Qjay said in how to select file and then print to printer ?:

          QPrintDialog: Cannot be used on non-native printers

          Well it would have been useful if you had explained that was the situation in the first place :)

          For PDF under Windows, read https://forum.qt.io/topic/5958/qprintdialog-error-on-qt-windows-environment/2:

          QPrintDialog can't be used with pdfs on Windows because it is a native print dialog and has no knowledge of Qt's PDF printer.

          1 Reply Last reply
          2
          • Q Offline
            Q Offline
            Qjay
            wrote on 9 Feb 2018, 13:41 last edited by Qjay 2 Sept 2018, 13:42
            #5

            sorry my bad :/ .

            so what are my options then ? what can i do

            P.S. i think that answer is way too old (2011) , there should be an update right ?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 9 Feb 2018, 15:11 last edited by mrjj 2 Sept 2018, 15:12
              #6

              Hi
              Since you want to print an EXISTING pdf file, its not
              possible directly with Qt.
              Qt can create a pdf file but its not possible to load/display/print
              external one as far as i know.
              (its outside Qt to display pdfs)
              So i think you need external lib for that.
              https://poppler.freedesktop.org/

              Can I ask you actual use case and what platform you need to support ?

              1 Reply Last reply
              6
              • Q Offline
                Q Offline
                Qjay
                wrote on 10 Feb 2018, 19:35 last edited by
                #7

                hey , the platform is windows (windows server) only . The actual use case is to print the pdf to a network printer or any printer available. the application will be on a RDP server.

                Why it's needed ?
                Guacamole client cannot print files. https://guacamole.apache.org/faq/#local-printers

                M 1 Reply Last reply 11 Feb 2018, 09:19
                1
                • Q Qjay
                  10 Feb 2018, 19:35

                  hey , the platform is windows (windows server) only . The actual use case is to print the pdf to a network printer or any printer available. the application will be on a RDP server.

                  Why it's needed ?
                  Guacamole client cannot print files. https://guacamole.apache.org/faq/#local-printers

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 11 Feb 2018, 09:19 last edited by
                  #8

                  @Qjay
                  hi
                  Ok, if you dont need processing in the Qt app , you could just use external tool to make it possible to print pdf via commandline

                  http://www.win10pdf.com/pdf-to-printer.html

                  or for a more complex one, use ghostscript.
                  https://stackoverflow.com/questions/2599925/how-to-print-pdf-on-default-network-printer-using-ghostscript-gswin32c-exe-she

                  some pdf viewers also support the print % options and could be used.

                  So you could use QProcess to start app that prints the pdf.

                  Q 1 Reply Last reply 12 Feb 2018, 03:43
                  4
                  • M mrjj
                    11 Feb 2018, 09:19

                    @Qjay
                    hi
                    Ok, if you dont need processing in the Qt app , you could just use external tool to make it possible to print pdf via commandline

                    http://www.win10pdf.com/pdf-to-printer.html

                    or for a more complex one, use ghostscript.
                    https://stackoverflow.com/questions/2599925/how-to-print-pdf-on-default-network-printer-using-ghostscript-gswin32c-exe-she

                    some pdf viewers also support the print % options and could be used.

                    So you could use QProcess to start app that prints the pdf.

                    Q Offline
                    Q Offline
                    Qjay
                    wrote on 12 Feb 2018, 03:43 last edited by
                    #9

                    @mrjj thanks , this looks like an awesome idea . I will try it out and will let you know !!

                    J 1 Reply Last reply 12 Feb 2018, 09:15
                    0
                    • Q Qjay
                      12 Feb 2018, 03:43

                      @mrjj thanks , this looks like an awesome idea . I will try it out and will let you know !!

                      J Offline
                      J Offline
                      JonB
                      wrote on 12 Feb 2018, 09:15 last edited by JonB 2 Dec 2018, 09:16
                      #10

                      @Qjay
                      This may not help because I do not know quite how you would do this directly from Qt, but when we want to print a PDF under Windows we use the underlying Win32 function ShellExecute() (https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx) to go (effectively) ShellExecute("print", native-path-to-file), and let Windows shell sort it out.

                      This is effectively the same as right-clicking on the file and picking the Print action. So you don't need to know anything about the actual process which does the printing, its arguments etc.

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jayden982
                        Banned
                        wrote on 22 Oct 2018, 22:09 last edited by
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        -1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved