Results 1 to 5 of 5

Thread: A question about querying printers

  1. #1
    Join Date
    Mar 2016
    Location
    northern california
    Posts
    3
    Thanks
    1
    Platforms
    MacOS X Windows

    Default A question about querying printers

    Hello, All:

    I have a question about printing, specifically about discovering print capabilities. My code first:
    Qt Code:
    1. int numPrinters = QPrinterInfo::availablePrinters().size();
    2. qDebug() << "Number of Printers=" << numPrinters;
    3. for( int i=0; i<numPrinters; ++i )
    4. {
    5. qDebug() << "Printer " << i << " name=" << QPrinterInfo::availablePrinters().at(i).printerName();
    6. QList<QPrinter::PaperSize> paperSizes = QPrinterInfo::availablePrinters().at(i).supportedPaperSizes();
    7. int numPapers = paperSizes.size();
    8. qDebug() << " numPapers=" << numPapers;
    9. QString temp;
    10. for( int j=0; j<numPapers; ++j)
    11. {
    12. QPrinter::PaperSize paper = paperSizes.at(j);
    13. temp += QString("%1 ").arg(paper);
    14. }
    15. qDebug() << " ->" << temp;
    16. }
    To copy to clipboard, switch view to plain text mode 
    And I get the following output (truncated to just one printer):
    Qt Code:
    1. Number of Printers= 9
    2. Printer 0 name= "Microsoft XPS Document Writer"
    3. numPapers= 106
    4. -> "2 2 29 28 3 30 4 8 0 0 9 19 1 27 30 30 30 30 30 25 30 30 30 30 30 30 26 30 24 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2 3 0 2 0 2 0 8 2 0 9 1 8 9 1 7 8 8 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 "
    To copy to clipboard, switch view to plain text mode 
    My first question is why is the paperSize duplicated to much? I can understand the "30", which refers to Custom, but why is "2" repeated 6 times? My second question is how to specify to the system which, eg, "2" or "30" I'm referring to? My third question how to query the specific page size for its actual size? I want to show the user the available print area. My last question is what is the best practice with respect to finding printer capabilities? Is there another way to get this information?

    Thank you,
    Eric.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: A question about querying printers

    Look at QPrinterInfo::supportedPageSizes() to return a list of QPageSize objects, then iterate over that list and QPageSize::name() gives you the "name" of the page size, and you can use other methods of QPageSize to get the actual dimensions if you need them.

    Here's a short example:

    Qt Code:
    1. QList<QPrinterInfo> pil = QPrinterInfo::availablePrinters();
    2. QStringList ul = {"mm", "p", "in", "pc", "dd", "ci"};
    3. for (int i = 0; i < pil.size(); i++)
    4. {
    5. QPrinterInfo pi = pil.at(i);
    6. qDebug("printer name=%s", qUtf8Printable(pi.printerName()));
    7. QList<QPageSize> psl = pi.supportedPageSizes();
    8. for (int j = 0; j < psl.size(); j++)
    9. {
    10. QPageSize psz = psl.at(j);
    11. QSizeF page_size = psz.definitionSize();
    12. QPageSize::Unit page_unit = psz.definitionUnits();
    13. qDebug("page size = %s (%.3f%s x %.3f%s)", qUtf8Printable(psz.name()), page_size.width(), qUtf8Printable(ul.at(page_unit)), page_size.height(), qUtf8Printable(ul.at(page_unit)));
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    P.S. I didn't find the abbreviation for a "cicero", so I chose "ci"...
    Last edited by jefftee; 11th March 2016 at 06:06.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Mar 2016
    Location
    northern california
    Posts
    3
    Thanks
    1
    Platforms
    MacOS X Windows

    Default Re: A question about querying printers

    Hello and thanks for the quick reply.

    I am not able to compile your code, however. I forgot to mention that I'm using VS10 under win7 and Qt4.8.7. Is what you posted Qt5.5 specific? It appears that "QPageSize" is. Do you have a Qt 4.8 solution that you could share?

    Thank you,
    Eric.

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: A question about querying printers

    Sorry, I don't use anything except the latest version of Qt. The doc will show which version of Qt introduced QPageSize, but it's obviously more recent than 4.8.7. I'll see if I can find Qt 4.8.7 doc later and see if there's something similar, but I assume you've already look at all of the documentation for Qt 4.8.7 QPrinterInfo and related classes and can't find anything, correct?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. The following user says thank you to jefftee for this useful post:

    ericL (23rd March 2016)

  6. #5
    Join Date
    Mar 2016
    Location
    northern california
    Posts
    3
    Thanks
    1
    Platforms
    MacOS X Windows

    Default Re: A question about querying printers

    Correct - I've read the documentation and found some of the parts, but not how to tie it altogether.

Similar Threads

  1. Replies: 2
    Last Post: 27th January 2016, 15:27
  2. Discovery of printers in the network?
    By surwassu in forum Newbie
    Replies: 27
    Last Post: 27th May 2011, 11:27
  3. QPrintDialog lists no printers
    By TMan in forum Qt Programming
    Replies: 6
    Last Post: 9th June 2009, 16:33
  4. Querying within Threads
    By bera82 in forum Qt Programming
    Replies: 11
    Last Post: 12th March 2008, 02:08

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.