我设法在QLabel上生成条形码。我尝试过QPainter,但无法正确对齐输出。标签的标签尺寸为50 * 25 mm,一行包含两个标签。
这是我打印标签的代码。
我想知道,有没有使用html打印标签的解决方案。所以我可以很好地设计。
QPrinter printer1;
QList<QPrinterInfo> printerList = QPrinterInfo::availablePrinters() ;
for(int r=0; r<printerList.size();++r)
{
if(printerList[r].printerName() == "TSC TE200")
{
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("Code 128");
barcodefont.setWeight(QFont::Normal);
barcodefont.setPointSize(60);
QFontMetrics fntm(barcodefont);
QPainter painter2;
if(!painter2.begin(&PRINTER))
return;
int x1 = printer1.paperRect().x() + printer1.width()/2 -
fntm.width("123456789")/2;enter code here
int y1 = printer1.paperRect().y();
int w1 = fntm.width("123456789");
int h1 = fntm.height()/4;
int x11 = printer1.paperRect().x() + printer1.width() - fntm.width("123456789")/2;
int y11 = printer1.paperRect().y();
int w11 = fntm.width("123456789");
int h11 = fntm.height()/4;
QRect rect10 = QRect(x1,y1,w1,h1);
QRect rect20 = QRect(x11,y11,w11,h11);
painter2.setFont(barcodefont);
painter2.drawText(rect10,Qt::AlignLeft,"123456789");
painter2.end();
break;
}
}
抱歉我的英文。我不会说流利的英语。提前谢谢。