Record a bug that QT connects to a Zebra printer

first describe the background

打印机:ZD888
打印方式:qpainter drawing and printing
纸张大小: 70mm*70mm

question

如果没有设置打印位置的偏移,就是这句代码QRectF textRect(0, 0, printer->width(), printer->height());,也就是这里的x,y是0,那么可以出纸,但是位置不对,如果不是零,比如是10,直接就不出纸,打印机指示灯闪烁两次 ,就没反应了。也找不到原因,完全无从下手的感觉,后来实在没办法了,我想尝试用wps去打印试试,谁成想,在设置纸张尺寸时候发现了问题,下面开始贴图解释

我圈起来的是wps从打印机读到的纸张尺寸

insert image description here

下面这张图是在斑马打印机设置软件里设置的纸张尺寸

insert image description here

可以看到,wps从打印机读取到的尺寸竟然比实际尺寸小了一毫米,我猜测额,wps应该是读取了打印机的dpi或其他参数换算出来的这个69mm,这中间应该是有四舍五入过,也就是按dpi计算出来的实际尺寸应该是69mm,斑马的这款软件应该也是对用户输入值70mm做过处理的,毕竟人家就是干这行的,只有我自己傻傻的在qt的代码里直接设置尺寸为70mm,后来把70换成69马上就OK了,一切都正常了,最后贴上出问题的代码

problematic code

//打印按钮下的代码
    QPrinter printer(QPrinter::HighResolution);
    //QPrinter printer;
    printer.setPrinterName(ui->cbx_printerList->currentText());
    // 设置纸张尺寸为70mm*70mm
    printer.setPageSize(QPrinter::Custom);
    qDebug()<<printer.widthMM();
    qDebug()<<printer.heightMM();
    qDebug()<<printer.margins().left;
    qDebug()<<printer.margins().top;
    qDebug()<<printer.margins().bottom;
    qDebug()<<printer.margins().right;
    printer.setPageSizeMM(QSizeF(70,70));
    //printer.setPageSize(QPageSize(QSizeF(70, 70), QPageSize::Millimeter));
    printer.setPageMargins(0,0,0,0,QPrinter::Millimeter);
    printer.setFullPage(true);

    QPrintPreviewDialog preview(&printer, this);
    connect(&preview, &QPrintPreviewDialog::paintRequested, this, &PrinterBaseSetup::printSlots);
    preview.exec();

//预览确认槽函数
 //创建画家对象,开始绘制
        QPainter painter(printer);
        // 设置字体和文本颜色
        QFont font("宋体", 10);
        painter.setFont(font);
        painter.setPen(Qt::black);

        // 绘制文本,注意要设置绘制区域和对齐方式
        //qDebug()<<"打印机默认宽高:"<<printer->width()<<","<<printer->height();
        QRectF textRect(100, 100, printer->width(), printer->height());
        painter.drawText(textRect, Qt::AlignTop, "今天是个好日子");

        // 结束绘制,释放资源
        painter.end();

epilogue

This word is used several times in this blog 猜测, which is a very imprecise thing for those engaged in technology, but there is no such thing, but I still have to say that with the development of computer science today, various programming languages, routes, and fields And so on, there are so many subdivisions, no one dares to say that he knows all the knowledge of a certain branch of computer science, but when facing actual projects, he will inevitably touch some knowledge that is not his professional field. Guessing and estimating can save time. I dare not say that it can save time, at least it can help us not waste time. In fact, it is no problem to sit down and finish QPainter and QPrinter, but the project delivery date will not be because I want to eat This knowledge is delayed.

And what is here 猜测is not really a guess. I prefer that it is an estimation of the problem at hand by the existing knowledge system in our brains. It is just that we have no way to describe the process of this estimation, and we are not even aware of it.

Guess you like

Origin blog.csdn.net/weixin_42485732/article/details/130452488