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. Printing without QPrintDialog (not on default printer)

Printing without QPrintDialog (not on default printer)

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 681 Views 1 Watching
  • 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.
  • A Offline
    A Offline
    addebito
    wrote on last edited by
    #1

    Hi,
    there is a way to print a page on a specific printer without the user interaction?

    I have 2 printers:
    Canon XXXX
    DYMO LabelWriter 450
    <-- this is the default printer

    Now I'd like to print on a Canon XXXX directly by code and without to show the QPrintDialog.
    How can I do that?

    I can iterate on QPrinterInfo list... but it doesn't return a QPrinter object.

      QList<QPrinterInfo> plist = QPrinterInfo::availablePrinters();
      qDebug() << "numbers of printers:" << plist.length();
      foreach (QPrinterInfo pinfo, plist) 
      {
        qDebug() << "printer name:" << pinfo.printerName();
      }
    
    JonBJ 1 Reply Last reply
    0
    • A addebito

      Hi,
      there is a way to print a page on a specific printer without the user interaction?

      I have 2 printers:
      Canon XXXX
      DYMO LabelWriter 450
      <-- this is the default printer

      Now I'd like to print on a Canon XXXX directly by code and without to show the QPrintDialog.
      How can I do that?

      I can iterate on QPrinterInfo list... but it doesn't return a QPrinter object.

        QList<QPrinterInfo> plist = QPrinterInfo::availablePrinters();
        qDebug() << "numbers of printers:" << plist.length();
        foreach (QPrinterInfo pinfo, plist) 
        {
          qDebug() << "printer name:" << pinfo.printerName();
        }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @addebito
      https://doc.qt.io/qt-5/qprinter.html#QPrinter-1 constructor takes a QPrinterInfo, which is what you have in your QPrinterInfo::availablePrinters() list.

      1 Reply Last reply
      2
      • A Offline
        A Offline
        addebito
        wrote on last edited by
        #3

        Oh my god, it was so easy...

            QString printerName       = "Canon XXXX";
            QList<QPrinterInfo> plist = QPrinterInfo::availablePrinters();
            foreach (QPrinterInfo pinfo, plist)
            {
                qDebug() << "printer name:" << pinfo.printerName();
                if (printerName.compare(pinfo.printerName()) == 0)
                {
                    QPrinter printer(pinfo);
                    m_report->printReport(&printer);
                    break;
                }
            }
        

        Thank you so much @JonB

        JonBJ 1 Reply Last reply
        0
        • A addebito

          Oh my god, it was so easy...

              QString printerName       = "Canon XXXX";
              QList<QPrinterInfo> plist = QPrinterInfo::availablePrinters();
              foreach (QPrinterInfo pinfo, plist)
              {
                  qDebug() << "printer name:" << pinfo.printerName();
                  if (printerName.compare(pinfo.printerName()) == 0)
                  {
                      QPrinter printer(pinfo);
                      m_report->printReport(&printer);
                      break;
                  }
              }
          

          Thank you so much @JonB

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @addebito
          Even simpler: you don't need to do the search yourself :) Just use static QPrinterInfo QPrinterInfo::printerInfo(const QString &printerName), https://doc.qt.io/qt-5/qprinterinfo.html#printerInfo !

          1 Reply Last reply
          2
          • A Offline
            A Offline
            addebito
            wrote on last edited by
            #5
                QString printerName = "Canon XXXX";
                QPrinterInfo pinfo  = QPrinterInfo::printerInfo(printerName);
                if (!pinfo.isNull())
                {
                    QPrinter printer(pinfo);
                    m_report->printReport(&printer);
                }
                else
                {
                    // show user message: printer not found...
                }
            

            Thanks again @JonB

            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