首页 > 解决方案 > 完成打印后如何从打印机获取 Qt 中的信号?

问题描述

我正在尝试在 Qt 中获取打印机的通知,但不幸的是找不到任何解决方案。我已经尝试检查状态,但它永远不会改变,它总是'PrinterState::Idle'。

void Functions::print(QString fileName)
{
    printerTmr = new QTimer(this); 
    printerTmr->setInterval(2000); 
    connect(printerTmr, SIGNAL(timeout()), this, SLOT(printerStateCheck())); 
    printerTmr->start(); //start checking the state of the printer

    printer.setPageSize(QPrinter::A6);
    printer.setOrientation(QPrinter::Landscape);
    QImage img(fileName);

    QSize size;
    QIcon icon;

    QPainter painter( &printer );
    int      w = printer.pageRect().width();
    int      h = printer.pageRect().height();
    QRect    page( 0, 0, w, h );

    QImage image(fileName);
    if (!image.isNull())
      icon.addPixmap(QPixmap::fromImage(image), QIcon::Normal, QIcon::On);

    icon = icon;
    size = QSize(w,h);
    QPixmap pixmap = icon.pixmap(size, QIcon::Normal, QIcon::On);
    ........

    //draw simulated landscape
    page.adjust( w/20, h/20, -w/20, -h/20 );
    painter.drawPixmap(QPoint(0,0),pixmap);

}

void Functions::printerStateCheck()
{

    if(printer.printerState() == QPrinter::PrinterState::Idle){
        qDebug()<<"PrinterState::Idle";
    }else if(printer.printerState() == QPrinter::PrinterState::Error){
        qDebug()<<"PrinterState::Error";
    }else if(printer.printerState() == QPrinter::PrinterState::Active){
        qDebug()<<"PrinterState::Active";
    }else if(printer.printerState() == QPrinter::PrinterState::Aborted){
        qDebug()<<"PrinterState::Aborted";
    }
}

标签: c++qt

解决方案


Qt 中的打印可能性非常简单。QPrinter设备代表一系列打印输出的页面,其使用方式与其他绘图设备(如QWidgetQPixmap )几乎完全相同。

在 Windows 或 macOS 上直接打印到打印机时,QPrinter 使用内置的打印机驱动程序。在 X11 上,QPrinter使用通用 Unix 打印系统 (CUPS) 将 PDF 输出发送到打印机。

作为替代方案,printProgram()函数可用于指定要使用的命令或实用程序,而不是系统默认值。(PS:但只在X11系统上进行pdf打印)

QPrinter::printerState()返回打印机的当前状态。这可能并不总是准确的(例如,如果打印机没有向操作系统报告其状态的能力)。

所以就像 Qt 文档所说的那样,它是在打印机、打印机驱动程序、打印子系统和操作系统本身上提供状态的。我认为你在 linux 下使用 CUPS 打印状态比在 windows 下更幸运。

尝试直接使用 OS 打印 API。

这是有关如何获取打印机和打印作业状态的WINAPI 示例代码的信息


推荐阅读