Search code examples
c++qtprintingcups

cups: How can I determine if the current / default printer is physically connected and turned on?


Is there a way to detect if a printer is online using libcups or Qt?

I am working on a program that requires a printer, and uses both libcups and QPrinter

I would like to give the user a nice error message if the current/default printer is not connected.

Right now, if the default printer is turned off (or not connected),

cups_dest_s printers = NULL;
int printerCount = cupsGetDests(&printers);

this crashes:

*** glibc detected *** /home/me/myApp/myApp: double free or corruption (out): 0x088501e0 ***

How can I determine if the current / default printer is physically connected and turned on ?


Solution

  • There is no accurate way to detect the state of printer as some printers don't have the capability of reporting their state to the operating system.

    Never the less you can get the current state of the printer by :

    PrinterState QPrinter::printerState() const
    

    Which returns Idle, Active, Aborted and Error. You can check if the printer state is Active or Idle. But there is no guarantee that your printer reports it's state correctly.