Cutting labels on a label printer to the correct size in Qt/C++?
-
I have a Brother QL700 label printer. I want to print a BarCode on this printer. I have successfully generated a barcode in code 39 format in QString and display this barcode on a QLabel->setText(QString). Now I want to print this barcode on a label printer. I am doing the following things but it does not give me the desired result.
QList<QPrinterInfo> printerList = QPrinterInfo::availablePrinters() ; for(int r=0; r<printerList.size();++r) { if(printerList[r].printerName() == "Brother_QL_700") { QPageSize pageSize(QSizeF(45.0,70.0),QPageSize::Millimeter,"",QPageSize::ExactMatch); QPrinter PRINTER(printerList[r],QPrinter::PrinterResolution); PRINTER.setOrientation(QPrinter::Portrait); PRINTER.setPageSize(pageSize); PRINTER.setFullPage(true); PRINTER.setOutputFormat(QPrinter::NativeFormat); int id = QFontDatabase::addApplicationFont("/Applications/untitledfolder/free3of9.ttf"); QFontDatabase::applicationFontFamilies(id).at(0); QFont barcodefont; barcodefont.setFamily("New"); barcodefont.setWeight(QFont::Normal); barcodefont.setPointSize(10); QFontMetrics fntm(barcodefont); QPainter painter2; if(!painter2.begin(&PRINTER)) return; int x1 = printer1.paperRect().x() + printer1.width()/2 - fntm.width(m_pDeviceInfoWgt->label->text())/2; int y1 = printer1.paperRect().y(); int w1 = fntm.width(m_pDeviceInfoWgt->label->text()); int h1 = fntm.height(); QRect rect10 = QRect(x1,y1,w1,h1); painter2.setFont(barcodefont); painter2.drawText(rect10,Qt::AlignCenter,"Sample_Text"); painter2.end(); break; } }
This code will print the barcode on the label printer, but does not cut the paper after 45 mm. It cuts the paper at 297mm (i.e. in A4 size of 210x297mm).
That means the printer assumes that the size of paper is QPrinter::A4 but I have given it a custom size. It should be printed using the size I specified, but it's not happening.
How can I properly tell the label printer to use the specified paper size so that it will cut the printed labels correctly?