PDA

View Full Version : A question about querying printers



ericL
11th March 2016, 02:52
Hello, All:

I have a question about printing, specifically about discovering print capabilities. My code first:

int numPrinters = QPrinterInfo::availablePrinters().size();
qDebug() << "Number of Printers=" << numPrinters;
for( int i=0; i<numPrinters; ++i )
{
qDebug() << "Printer " << i << " name=" << QPrinterInfo::availablePrinters().at(i).printerNam e();
QList<QPrinter::PaperSize> paperSizes = QPrinterInfo::availablePrinters().at(i).supportedP aperSizes();
int numPapers = paperSizes.size();
qDebug() << " numPapers=" << numPapers;
QString temp;
for( int j=0; j<numPapers; ++j)
{
QPrinter::PaperSize paper = paperSizes.at(j);
temp += QString("%1 ").arg(paper);
}
qDebug() << " ->" << temp;
}
And I get the following output (truncated to just one printer):


Number of Printers= 9
Printer 0 name= "Microsoft XPS Document Writer"
numPapers= 106
-> "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 "

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.

jefftee
11th March 2016, 04:35
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:



QList<QPrinterInfo> pil = QPrinterInfo::availablePrinters();
QStringList ul = {"mm", "p", "in", "pc", "dd", "ci"};
for (int i = 0; i < pil.size(); i++)
{
QPrinterInfo pi = pil.at(i);
qDebug("printer name=%s", qUtf8Printable(pi.printerName()));
QList<QPageSize> psl = pi.supportedPageSizes();
for (int j = 0; j < psl.size(); j++)
{
QPageSize psz = psl.at(j);
QSizeF page_size = psz.definitionSize();
QPageSize::Unit page_unit = psz.definitionUnits();
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)));
}
}


P.S. I didn't find the abbreviation for a "cicero", so I chose "ci"... :)

ericL
11th March 2016, 18:48
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.

jefftee
11th March 2016, 20:33
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?

ericL
11th March 2016, 23:35
Correct - I've read the documentation and found some of the parts, but not how to tie it altogether.