我正在尝试在 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";
}
}
最佳答案
Qt 中的打印功能非常简单。 QPrinter device 表示一系列打印输出页面,其使用方式与其他绘图设备(例如 QWidget)几乎完全相同。和 QPixmap .
在 Windows 或 macOS 上直接打印到打印机时,QPrinter 使用内置打印机驱动程序。在 X11 上,QPrinter使用通用 Unix 打印系统 (CUPS) 将 PDF 输出发送到打印机。
作为替代方案,printProgram()函数可用于指定要使用的命令或实用程序,而不是系统默认值。 (P.S.:但只能在X11系统上进行pdf打印)
QPrinter::printerState()返回打印机的当前状态。这可能并不总是准确的(例如,如果打印机没有向操作系统报告其状态的能力)。
所以就像 Qt 文档所说的那样,它在打印机、打印机驱动程序、打印子系统和操作系统本身上提供状态。我认为你在 linux 下使用 CUPS 打印状态比在 windows 下更幸运。
尝试直接使用 OS 打印 API。
这是关于 How to get the status of a printer and a print job 的 WINAPI 示例代码的信息
关于c++ - 打印完成后如何从打印机获取 Qt 信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56700777/