1

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;
}
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Helg1980
  • 47
  • 1
  • 9
  • 1
    CUPS doesn't know which one will print. You could get the name of the default printer via `cupsGetDefault` or you could ask the user to choose a working printer from the list. – trokymchuk Sep 01 '22 at 12:50
  • The problem is that the KDE print dialog called via D-Bus does not return the name of the selected printer. – Helg1980 Sep 01 '22 at 15:50

1 Answers1

1

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:

  • cupsGetDest (param name: NULL for the default destination)
  • cupsGetDests2 (param http: Connection to server or CUPS_HTTP_DEFAULT)

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)

Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11
  • Thanks! I will try both methods. I'm trying to figure out how to add some dummy printer in addition to pdf in linux so that it can be seen which of the two is selected. – Helg1980 Sep 01 '22 at 13:57