Qt在window系统下打印小票——————附带完整代码
生活随笔
收集整理的這篇文章主要介紹了
Qt在window系统下打印小票——————附带完整代码
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 0 背景
- 1 打印方法
- 1.1 直接調(diào)用打印設(shè)置界面進(jìn)行打印
- 1.2 直接打印,不出現(xiàn)打印設(shè)置界面
- 2 打印類(lèi)
- 3 打印類(lèi)的一些測(cè)試
- 3.1 測(cè)試字體和上下間隙
- 3.2 測(cè)試容納字體的最大寬度
- 4 彩蛋
0 背景
因?yàn)樾枰玫酱蛴⌒∑钡墓δ?#xff0c;因此通過(guò)查詢網(wǎng)上相關(guān)資料和自己動(dòng)手實(shí)踐整理出此文。希望對(duì)需要用到這方面的朋友有幫助。
先放結(jié)果:
為了打印這張小票,自己也打印了上百?gòu)埿∑?#xff0c;用來(lái)測(cè)試字體大小、間距、樣式等。
1 打印方法
在pro文件中加入
QT += printsupport #打印1.1 直接調(diào)用打印設(shè)置界面進(jìn)行打印
QPrinter* printer = printDialog.printer();PainterReceipt painterReceipt(printer, m_transactionJsonData);painterReceipt.printGoodsInformation();其中,上面的m_transactionJsonData為QJsonObject類(lèi)型
QJsonObject m_transactionJsonData;1.2 直接打印,不出現(xiàn)打印設(shè)置界面
QPrinterInfo targetPrinter = QPrinterInfo::printerInfo(g_printerName);QPrinter printer(targetPrinter);PainterReceipt painterReceipt(&printer, m_transactionJsonData);for(int i = 0;i < g_receiptAmount;++i){//打印小票painterReceipt.printGoodsInformation();}其中,上面的g_printerName為打印機(jī)的名字,g_receiptAmount為打印小票的數(shù)量【自己設(shè)置】。
獲得打印機(jī)名字的方法:
QPrinterInfo printerInfo;QStringList printerList = printerInfo.availablePrinterNames();2 打印類(lèi)
.h文件
#ifndef PAINTERRECEIPT_H #define PAINTERRECEIPT_H#include<QPainter> #include<QtPrintSupport/QPrintDialog> #include<QtPrintSupport/QPrinter> #include<QPaintEvent> #include<QJsonObject>class PainterReceipt : public QPainter { public:PainterReceipt(QPrinter* printer, QJsonObject goodsInfo);~PainterReceipt();void printGoodsInformation(); private:QJsonObject m_goodsInforamtion; };#endif // PAINTERRECEIPT_H.cpp文件
#include "painterreceipt.h"#include"Global.h"//全局變量 #include<QJsonArray> #include<QJsonValue> #include<QSqlQuery> #include<QSqlDatabase> #include<QPrintDialog>PainterReceipt::PainterReceipt(QPrinter *printer, QJsonObject goodsInfo):QPainter(printer),m_goodsInforamtion(goodsInfo) {}PainterReceipt::~PainterReceipt() {} //打印小票信息 void PainterReceipt::printGoodsInformation() {int paperMaxWidth = 182;int spacingHeight = 15;//漢字:15個(gè);大寫(xiě)小字母30;數(shù)字30QString shopName = "零售終端測(cè)試軟件";int hanziMaxNumber = 15;int figureMaxNumber = 30;QString cuttingLine = QString("%1").arg("", figureMaxNumber, QLatin1Char('*'));//QFontMetrics metrics(QFont(""));//int shopNameWidth = metrics.horizontalAdvance(shopName);//qDebug()<<"shopNameWidth:"<<shopNameWidth;QFont font1;font1.setPointSize(9);this->setFont(font1);QRect rect(0, 0, paperMaxWidth, spacingHeight);//圖片QPixmap shopImage;shopImage.load(":/Image/logo.png");shopImage.scaled(QSize(182, 60), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setWidth(paperMaxWidth);rect.setHeight(60);this->drawPixmap(rect ,shopImage);//店鋪名rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setWidth(paperMaxWidth);rect.setHeight(spacingHeight);this->drawText(rect,Qt::AlignCenter, shopName);// 工號(hào)QString account = m_goodsInforamtion.value("accountId").toString();rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);//this->drawText(rect,Qt::AlignLeft, "工號(hào):"+account.mid(0, 2) + "****" + account.mid(7,10));//工號(hào):188****1212//單號(hào)QString clientTransCode = m_goodsInforamtion.value("clientTransCode").toString();rect.setY(rect.y() + rect.height());rect.setHeight(spacingHeight);this->drawText(rect,Qt::AlignLeft, "單號(hào):"+clientTransCode.mid(0, 9) + "***" + clientTransCode.mid(20, 31));//單號(hào):1234567890***123456789012//時(shí)間rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);rect.setWidth(180);this->drawText(rect,Qt::AlignLeft, "日期:" + m_goodsInforamtion.value("clientTime").toString());//QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")// 分割線rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);rect.setWidth(paperMaxWidth);this->drawText(rect, Qt::AlignLeft, cuttingLine);//品名 數(shù)量 單價(jià) 小計(jì)rect.setY(rect.y() + rect.height());rect.setHeight(spacingHeight);this->drawText(rect,Qt::AlignLeft,"品名");rect.setX(paperMaxWidth*0.285);//paperMaxWidth/8*3this->drawText(rect,Qt::AlignLeft,"單價(jià)");rect.setX(paperMaxWidth*0.560);//paperMaxWidth/8*5this->drawText(rect,Qt::AlignLeft,"數(shù)量");this->drawText(rect,Qt::AlignRight,"金額");rect.setX(0);if(m_goodsInforamtion.contains("listSubTransDetail")){QJsonValue goodsInforamtionValue= m_goodsInforamtion.value("listSubTransDetail");if(goodsInforamtionValue.isArray()){QJsonArray goodsInforamtionArray = goodsInforamtionValue.toArray();for(int i = 0;i < goodsInforamtionArray.size();++i){QJsonValue value = goodsInforamtionArray.at(i);rect.setX(0);//打印商品名稱QString goodsName = value["comName"].toString();QString productName;if(goodsName.size() > hanziMaxNumber){while(goodsName.size() != 0){productName = goodsName.mid(0, hanziMaxNumber);qDebug()<<"productName:"<<productName;goodsName = goodsName.mid(hanziMaxNumber, goodsName.size() - hanziMaxNumber);qDebug()<<"iter.goodsNames:"<<goodsName;rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);this->drawText(rect, Qt::AlignLeft, productName);}}else{rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);this->drawText(rect, Qt::AlignLeft, goodsName);}//打印序號(hào)、商品單價(jià)、數(shù)量、小計(jì)rect.setY(rect.y()+ rect.height());rect.setX(0);rect.setHeight(spacingHeight);rect.setWidth(18);this->drawText(rect, Qt::AlignLeft, QString::number(i + 1));//商品單價(jià)rect.setX(19);//74rect.setHeight(spacingHeight);rect.setWidth(55);this->drawText(rect, Qt::AlignRight, QString::number(value["transPrice"].toDouble(), 'f', 2));//數(shù)量rect.setX(76);rect.setHeight(spacingHeight);rect.setWidth(49);//125this->drawText(rect, Qt::AlignRight, QString::number(value["transNumber"].toDouble(), 'f', 2));//小計(jì)rect.setX(127);rect.setHeight(spacingHeight);rect.setWidth(55);//182this->drawText(rect, Qt::AlignRight, QString::number(value["transTotal"].toDouble(), 'f', 2));}}}// 分割線rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);rect.setWidth(paperMaxWidth);this->drawText(rect, Qt::AlignLeft, cuttingLine); //總計(jì)rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);rect.setWidth(paperMaxWidth);this->drawText(rect,Qt::AlignLeft, "總計(jì):");rect.setHeight(spacingHeight);rect.setWidth(paperMaxWidth);this->drawText(rect,Qt::AlignRight, QString::number(m_goodsInforamtion.value("transTotal").toDouble(), 'f', 2));//m_goodsInforamtion.value("transTotal") //付款方式qDebug()<<"payChannel:"<<m_goodsInforamtion.value("payChannel").toString();QString paymentMethod = m_goodsInforamtion.value("payChannel").toString();QSqlQuery query(QSqlDatabase::database("connection1"));query.exec(QString("SELECT payChanelName FROM PayChannelInfo WHERE payChanelFlag = \'%1\'").arg(paymentMethod));QString paymentName;while(query.next()){qDebug()<<"paymentName:"<<query.value(0).toString();paymentName = query.value(0).toString();}rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);this->drawText(rect,Qt::AlignLeft, "付款方式:"+ paymentName);//只針對(duì)現(xiàn)金//實(shí)收rect.setX(0);rect.setWidth(40);rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);this->drawText(rect,Qt::AlignLeft, "實(shí)收:");rect.setX(40);rect.setHeight(spacingHeight);rect.setWidth(142);this->drawText(rect,Qt::AlignRight, g_actualPaymentAmount);//找零if(paymentName == "現(xiàn)金支付"){rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setWidth(60);rect.setHeight(spacingHeight);this->drawText(rect,Qt::AlignLeft, "現(xiàn)金找零:");rect.setX(60);rect.setWidth(122);rect.setHeight(spacingHeight);this->drawText(rect,Qt::AlignRight, g_change);}if(g_customPrintContentOne.size() != 0 || g_customPrintContentTwo.size() != 0 || g_customPrintContentThree.size() != 0){// 分割線rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);rect.setWidth(paperMaxWidth);this->drawText(rect, Qt::AlignLeft, cuttingLine);// 內(nèi)容if(g_customPrintContentOne.size() != 0){rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);rect.setWidth(paperMaxWidth);this->drawText(rect, Qt::AlignLeft, g_customPrintContentOne);}// 內(nèi)容if(g_customPrintContentTwo.size() != 0){rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);rect.setWidth(paperMaxWidth);this->drawText(rect, Qt::AlignLeft, g_customPrintContentTwo);}// 內(nèi)容if(g_customPrintContentThree.size() != 0){rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);rect.setWidth(paperMaxWidth);this->drawText(rect, Qt::AlignLeft, g_customPrintContentThree);}}}3 打印類(lèi)的一些測(cè)試
3.1 測(cè)試字體和上下間隙
doTestFont() {QFont font1;font1.setPointSize(5);QRect rect(0, 0, 800, 7);this->setFont(font1);this->drawText(rect,Qt::AlignLeft,"5號(hào)字體:1234567890123456789012345678901234567890");font1.setPointSize(6);this->setFont(font1);rect.setY(rect.y()+ rect.height());rect.setHeight(8);this->drawText(rect,Qt::AlignLeft,"6號(hào)字體:1234567890123456789012345678901234567890");font1.setPointSize(7);this->setFont(font1);rect.setY(rect.y()+ rect.height());rect.setHeight(9);this->drawText(rect,Qt::AlignLeft,"7號(hào)字體:1234567890123456789012345678901234567890");font1.setPointSize(8);this->setFont(font1);rect.setY(rect.y()+ rect.height());rect.setHeight(10);this->drawText(rect,Qt::AlignLeft,"8號(hào)字體:1234567890123456789012345678901234567890");font1.setPointSize(9);this->setFont(font1);rect.setY(rect.y()+ rect.height());rect.setHeight(11);this->drawText(rect,Qt::AlignLeft,"9號(hào)字體:1234567890123456789012345678901234567890");font1.setPointSize(10);this->setFont(font1);rect.setY(rect.y()+ rect.height());rect.setHeight(12);this->drawText(rect,Qt::AlignLeft,"10號(hào)字體:1234567890123456789012345678901234567890");font1.setPointSize(10);this->setFont(font1);rect.setY(rect.y()+ rect.height());rect.setHeight(12);this->drawText(rect,Qt::AlignLeft, QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); }3.2 測(cè)試容納字體的最大寬度
doTestWidth() {QFont font1;font1.setPointSize(9);this->setFont(font1);QRect rect(0, 0, 182, 15);this->drawText(rect,Qt::AlignLeft,"一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十");rect.setY(rect.y()+ rect.height());// rect.setWidth(183);rect.setHeight(15);this->drawText(rect,Qt::AlignLeft,"asdfghjklqwertyuiopzxcvbnmqwertyuiopzxcvbnm");rect.setY(rect.y()+ rect.height());//rect.setWidth(184);rect.setHeight(15);this->drawText(rect,Qt::AlignLeft,"ASDFGHJKLQWERTYUIOPZXCVBNMZXCVBNMPOIUYTREWQ");rect.setY(rect.y()+ rect.height());//rect.setWidth(185);rect.setHeight(15);this->drawText(rect,Qt::AlignLeft,"12345678901234567890123456789012345678901234567");// rect.setY(rect.y()+ rect.height()); // // rect.setWidth(186); // rect.setHeight(15); // this->drawText(rect,Qt::AlignLeft,"15字體:1234567890123456789012345678901234567890");// rect.setY(rect.y()+ rect.height()); // //rect.setWidth(187); // rect.setHeight(16); // this->drawText(rect,Qt::AlignLeft,"16字體:1234567890123456789012345678901234567890");// rect.setY(rect.y()+ rect.height()); // //rect.setWidth(188); // rect.setHeight(17); // this->drawText(rect,Qt::AlignLeft,"17字體:1234567890123456789012345678901234567890");rect.setY(rect.y()+ rect.height());rect.setWidth(182);rect.setHeight(15);this->drawText(rect,Qt::AlignLeft, QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); }4 彩蛋
當(dāng)時(shí)配置好打印機(jī)的驅(qū)動(dòng),打印出了一張測(cè)試小票。
緊接著打印出了自己的第一張小票
總結(jié)
以上是生活随笔為你收集整理的Qt在window系统下打印小票——————附带完整代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 江苏自考计算机应用基础停考了吗,江苏自考
- 下一篇: 关于photoswipe的data-si