Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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?

    code:

    mainwindow.h:

    #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
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    
         QPrinter printer;
    
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    
    
        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";
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    

    main.cpp:

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

  • Lifetime Qt Champion

    Hi,

    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