Important: Please read the Qt Code of Conduct -

How to know a specific printer errors and how to handle them?

  • Hi everyone
    I am working on a simple project where I want to know the available printers, then connect to one of them and see if there are errors or not.
    I have used QPrinterInfo class in order to know the available printer names, then in order to work with one of the available printers, I set its name using "printer.setPrinterName("desired printer name")".
    In order to know if there are some errors related to this printer (named as desired printer name), I searched and find out I have to use QPrinter::Error and then handle the errors.
    My question is that what kinds of error can occur when using QPrinter::Error inside my if statement(in the following code)? If no papers exists inside the printer, will using QPrinter::Error throw an error? In general, how can I know what kinds of error I can handle and how to handle them?



    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QtPrintSupport/QPrinter>
    #include <QtPrintSupport/QPrinterInfo>
    #include <QtPrintSupport/QPrintDialog>
    #include <QList>
    namespace Ui {
    class MainWindow;
    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = nullptr);
         QPrinter printer;
        Ui::MainWindow *ui;
    #endif // MAINWINDOW_H


    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        QPrinterInfo PrinterInfo;
        qDebug() << QStringList(QPrinterInfo::availablePrinterNames());
        if (QPrinterInfo::availablePrinterNames().isEmpty())
            qDebug() << " printer not found ";
        printer.setPrinterName("desired printer name");
        if (printer.printerState() == QPrinter::Error)
            qDebug() << "there is an error";
        delete ui;


    #include "mainwindow.h"
    #include <QApplication>
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        MainWindow w;;
        return a.exec();

  • Lifetime Qt Champion


    AFAIK, the module does not provide that level of details. You should redirect your users to the system settings where that kind of information should be available.

    In any case, the state returned value will not throw any exception.

Log in to reply