Qt在window系统下打印小票——————附带完整代码
生活随笔
收集整理的這篇文章主要介紹了
Qt在window系统下打印小票——————附带完整代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 0 背景
- 1 打印方法
- 1.1 直接調用打印設置界面進行打印
- 1.2 直接打印,不出現打印設置界面
- 2 打印類
- 3 打印類的一些測試
- 3.1 測試字體和上下間隙
- 3.2 測試容納字體的最大寬度
- 4 彩蛋
0 背景
因為需要用到打印小票的功能,因此通過查詢網上相關資料和自己動手實踐整理出此文。希望對需要用到這方面的朋友有幫助。
先放結果:
為了打印這張小票,自己也打印了上百張小票,用來測試字體大小、間距、樣式等。
1 打印方法
在pro文件中加入
QT += printsupport #打印1.1 直接調用打印設置界面進行打印
QPrinter* printer = printDialog.printer();PainterReceipt painterReceipt(printer, m_transactionJsonData);painterReceipt.printGoodsInformation();其中,上面的m_transactionJsonData為QJsonObject類型
QJsonObject m_transactionJsonData;1.2 直接打印,不出現打印設置界面
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為打印機的名字,g_receiptAmount為打印小票的數量【自己設置】。
獲得打印機名字的方法:
QPrinterInfo printerInfo;QStringList printerList = printerInfo.availablePrinterNames();2 打印類
.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個;大寫小字母30;數字30QString shopName = "零售終端測試軟件";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);// 工號QString account = m_goodsInforamtion.value("accountId").toString();rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);//this->drawText(rect,Qt::AlignLeft, "工號:"+account.mid(0, 2) + "****" + account.mid(7,10));//工號:188****1212//單號QString clientTransCode = m_goodsInforamtion.value("clientTransCode").toString();rect.setY(rect.y() + rect.height());rect.setHeight(spacingHeight);this->drawText(rect,Qt::AlignLeft, "單號:"+clientTransCode.mid(0, 9) + "***" + clientTransCode.mid(20, 31));//單號:1234567890***123456789012//時間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);//品名 數量 單價 小計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,"單價");rect.setX(paperMaxWidth*0.560);//paperMaxWidth/8*5this->drawText(rect,Qt::AlignLeft,"數量");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);}//打印序號、商品單價、數量、小計rect.setY(rect.y()+ rect.height());rect.setX(0);rect.setHeight(spacingHeight);rect.setWidth(18);this->drawText(rect, Qt::AlignLeft, QString::number(i + 1));//商品單價rect.setX(19);//74rect.setHeight(spacingHeight);rect.setWidth(55);this->drawText(rect, Qt::AlignRight, QString::number(value["transPrice"].toDouble(), 'f', 2));//數量rect.setX(76);rect.setHeight(spacingHeight);rect.setWidth(49);//125this->drawText(rect, Qt::AlignRight, QString::number(value["transNumber"].toDouble(), 'f', 2));//小計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); //總計rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);rect.setWidth(paperMaxWidth);this->drawText(rect,Qt::AlignLeft, "總計:");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);//只針對現金//實收rect.setX(0);rect.setWidth(40);rect.setY(rect.y()+ rect.height());rect.setHeight(spacingHeight);this->drawText(rect,Qt::AlignLeft, "實收:");rect.setX(40);rect.setHeight(spacingHeight);rect.setWidth(142);this->drawText(rect,Qt::AlignRight, g_actualPaymentAmount);//找零if(paymentName == "現金支付"){rect.setX(0);rect.setY(rect.y()+ rect.height());rect.setWidth(60);rect.setHeight(spacingHeight);this->drawText(rect,Qt::AlignLeft, "現金找零:");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);// 內容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);}// 內容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);}// 內容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 打印類的一些測試
3.1 測試字體和上下間隙
doTestFont() {QFont font1;font1.setPointSize(5);QRect rect(0, 0, 800, 7);this->setFont(font1);this->drawText(rect,Qt::AlignLeft,"5號字體:1234567890123456789012345678901234567890");font1.setPointSize(6);this->setFont(font1);rect.setY(rect.y()+ rect.height());rect.setHeight(8);this->drawText(rect,Qt::AlignLeft,"6號字體:1234567890123456789012345678901234567890");font1.setPointSize(7);this->setFont(font1);rect.setY(rect.y()+ rect.height());rect.setHeight(9);this->drawText(rect,Qt::AlignLeft,"7號字體:1234567890123456789012345678901234567890");font1.setPointSize(8);this->setFont(font1);rect.setY(rect.y()+ rect.height());rect.setHeight(10);this->drawText(rect,Qt::AlignLeft,"8號字體:1234567890123456789012345678901234567890");font1.setPointSize(9);this->setFont(font1);rect.setY(rect.y()+ rect.height());rect.setHeight(11);this->drawText(rect,Qt::AlignLeft,"9號字體:1234567890123456789012345678901234567890");font1.setPointSize(10);this->setFont(font1);rect.setY(rect.y()+ rect.height());rect.setHeight(12);this->drawText(rect,Qt::AlignLeft,"10號字體: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 測試容納字體的最大寬度
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 彩蛋
當時配置好打印機的驅動,打印出了一張測試小票。
緊接著打印出了自己的第一張小票
總結
以上是生活随笔為你收集整理的Qt在window系统下打印小票——————附带完整代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 江苏自考计算机应用基础停考了吗,江苏自考
- 下一篇: java信息管理系统总结_java实现科