c++linuxprintingcups

How to programmatically get the name of the current printer through the libcups in Linux?


I'm trying to get the name of the current printer using the libcups library in Linux, but I can't find such a method. I found only how to get a complete list of printers, but how to find out which one will print is not clear.

#include <cups/cups.h>

QStringList getPrinters()
{    
   QStringList printerNames;
   cups_dest_t *dests;
   int num_dests = cupsGetDests(&dests);
   for (int pr = 0; pr < num_dests; ++pr) {
      QString printerName = QString::fromUtf8(dests[pr].name);
      printerNames.append(printerName);
   }
   cupsFreeDests(num_dests, dests);
   return printerNames;
}

Solution

  • Once you have a valid destination (cups_dest_t), one can retrieve the informations via: cupsGetOption

    Example (from https://openprinting.github.io/cups/doc/cupspm.html#basic-destination-information):

    const char *model = cupsGetOption("printer-make-and-model",
                                      dest->num_options,
                                      dest->options);
    

    To find the default printer one can use:

    Other suggestion would be:

    Last but not least:


    Sidenote:

    Since you're using Qt, doesn't Qt have printer support?

    E.g.

    QPrinter::QPrinter(const QPrinterInfo &printer, QPrinter::PrinterMode mode = ScreenResolution);
    

    (see https://doc.qt.io/qt-6/qprinter.html#QPrinter-1)

    and

    bool QPrinterInfo::isDefault() const;
    

    (see https://doc.qt.io/qt-6/qprinterinfo.html#isDefault)