qt实现小票打印
1.需求
需要實(shí)現(xiàn)訂單結(jié)算后的小票打印,要求能打印商品數(shù)據(jù)和結(jié)算數(shù)據(jù)及將小票號(hào)打印成條碼,退貨或查詢時(shí)能通過(guò)掃描槍掃描條碼快速查詢到訂單信息
2.解決
1.通過(guò)將數(shù)據(jù)轉(zhuǎn)化為html后調(diào)用qt進(jìn)行打印,打印機(jī)無(wú)法打印img標(biāo)簽中的條碼
2.通過(guò)將數(shù)據(jù)轉(zhuǎn)化為html,將條碼直接繪制的方法實(shí)現(xiàn)了需求
3.代碼
Barcode.h
// Barcode.h: interface for the CBarcode class. // // Copyright 2002 Neil Van Eps // ////////////////////////////////////////////////////////////////////// #ifndef BARCODE_H #define BARCODE_H #include <QString> #include <QPainter>// bblib.h // must put build number define on third line #define BUILDNUM 7 // // // Copyright 2002 Neil Van Eps //enum Symbology {RATIONALCODABAR,TRADITIONALCODABAR,I2OF5,CODE39,COD128,UPCEAN,CODE93 };#define COLORWHITE 0x00FFFFFF #define COLORBLACK 0x00000000class CBarcode { public:CBarcode();void LoadData(QString csMessage, const QString &strImagePath, double dNarrowBar, double dFinalHeight, int nStartingXPixel, int nStartingYPixel, double dRatio = 1.0);virtual void DrawBitmap(QPainter *painter = nullptr) = 0;//virtual void BitmapToClipboard() = 0;virtual ~CBarcode();long GetBarcodePixelWidth();long GetBarcodePixelHeight(); protected:QString m_csMessage;//HDC m_hDC; QString m_strImagePath;long m_nFinalBarcodePixelWidth;long m_nNarrowBarPixelWidth;long m_nPixelHeight;long m_nStartingXPixel;long m_nStartingYPixel;long m_nSymbology;long m_nWideBarPixelWidth;virtual void DrawPattern(QString csPattern, QPainter *painter) = 0; };#endif // !defined(AFX_BARCODE_H__C5D7FCDA_5C8F_4244_AF95_33D6FA93F8EB__INCLUDED_)Barcode.cpp
// Barcode.cpp: implementation of the CBarcode class. // // Copyright 2002 Neil Van Eps // ////////////////////////////////////////////////////////////////////// #include "Barcode.h"////////////////////////////////////////////////////////////////////// // Construction/Destruction //////////////////////////////////////////////////////////////////////CBarcode::CBarcode() {}CBarcode::~CBarcode() {}//////////////////////////////////////////////////////////////////////////////////// // // Name: // LoadData() // // Description: // // // Arguments: // // // Return: // // // Called by: // // //////////////////////////////////////////////////////////////////////////////////// void CBarcode::LoadData(QString csMessage, const QString &strImagePath, double dNarrowBar, double dFinalHeight, int nStartingXPixel, int nStartingYPixel, double dRatio) {int i, nXAxisDpi, nYAxisDpi, nTemp;char c;// values that can be saved without translationm_csMessage = csMessage;m_nStartingXPixel = nStartingXPixel;//nStartingXPixel*10;m_nStartingYPixel = nStartingYPixel;//nStartingYPixel*10;//m_hDC = pDC;m_strImagePath = strImagePath;// load the final attributes that depend on the device contextm_nPixelHeight = (int)dFinalHeight;//(int)((10*dFinalHeight) + 0.5);m_nNarrowBarPixelWidth = (int)(dNarrowBar + 0.5);m_nWideBarPixelWidth = (int)(dRatio*m_nNarrowBarPixelWidth);switch (m_nSymbology){case CODE39:// get final character width//nTemp = m_csMessage.GetLength() + 2;nTemp = m_csMessage.length() + 2;// add messagem_nFinalBarcodePixelWidth = nTemp * ((3 * m_nWideBarPixelWidth) + (7 * m_nNarrowBarPixelWidth));break;case COD128:// get final character width//nTemp = m_csMessage.GetLength();nTemp = m_csMessage.length();m_nFinalBarcodePixelWidth = ((nTemp * 11) + 35)*m_nNarrowBarPixelWidth;break;case CODE93:// get final character width//nTemp = m_csMessage.GetLength();nTemp = m_csMessage.length();m_nFinalBarcodePixelWidth = (((nTemp + 4) * 9) + 1)*m_nNarrowBarPixelWidth;case UPCEAN:case TRADITIONALCODABAR:break;}return; } long CBarcode::GetBarcodePixelHeight() {return m_nPixelHeight; }long CBarcode::GetBarcodePixelWidth() {return m_nFinalBarcodePixelWidth; }Code39.h
// Code39.h: interface for the CCode39 class. // // Copyright 2002 Neil Van Eps // ////////////////////////////////////////////////////////////////////// #ifndef CODE39_H #define CODE39_H#include "Barcode.h"class CCode39 : public CBarcode { public://void BitmapToClipboard();void DrawBitmap(QPainter *painter = nullptr);CCode39();virtual ~CCode39(); private:void DrawPattern(QString csPattern, QPainter *painter);QString RetrievePattern(char c);};#endif // !defined(AFX_CODE39_H__6FE17747_EADF_4E89_9DCF_7688B04897BC__INCLUDED_)Code39.cpp
// Code39.cpp: implementation of the CCode39 class. // // Copyright 2002 Neil Van Eps // ////////////////////////////////////////////////////////////////////// #include "Code39.h" #include <QPainter> #include <QImage> #include <QPen> #include <QChar> ////////////////////////////////////////////////////////////////////// // Construction/Destruction //////////////////////////////////////////////////////////////////////CCode39::CCode39() {// code 39m_nSymbology = CODE39; }CCode39::~CCode39() {}//////////////////////////////////////////////////////////////////////////////////// // // Name: // DrawBitmap() // // Description: // draws a barcode using the previously loaded data // // Arguments: // none // // Return: // void // // Called by: // public class interface // //////////////////////////////////////////////////////////////////////////////////// //這里有一個(gè)問(wèn)題 我怎么搞到圖片大小呢 void CCode39::DrawBitmap(QPainter *painter) {int i;QString csCurrentPattern;DrawPattern(RetrievePattern('*'), painter);// draw each character in the messagefor (i=0;i<m_csMessage.length();i++)DrawPattern(RetrievePattern(m_csMessage.at(i).toLatin1()), painter);// draw stop character, also an asteriskDrawPattern(RetrievePattern('*'), painter);return; }//////////////////////////////////////////////////////////////////////////////////// // // Name: // DrawPattern() // // Description: // draws the passed character pattern at the end of the barcode // // Arguments: // CString csPattern - the bar pattern to draw // // Return: // void // // Called by: // CRationalCodabar::DrawBitmap() // ////////////////////////////////////////////////////////////////////////////////////void CCode39::DrawPattern( QString csPattern ,QPainter *painter) {int i, nXPixel, nYPixel, nTempWidth;QPen penbar = QPen(Qt::black);penbar.setWidth(1);penbar.setStyle(Qt::SolidLine);QPen penspace = QPen(Qt::white);penspace.setWidth(1);penspace.setStyle(Qt::SolidLine);painter->setRenderHint(QPainter::Antialiasing, true);// initialize X pixel valuenXPixel = m_nStartingXPixel;for (i = 0; i < csPattern.length(); i++){// decide if narrow or wide barif (csPattern.at(i) == L'n')nTempWidth = m_nNarrowBarPixelWidth;elsenTempWidth = m_nWideBarPixelWidth;// X value for loopfor (nXPixel = m_nStartingXPixel; nXPixel < m_nStartingXPixel + nTempWidth; nXPixel++){if (i % 2 == 0)painter->setPen(penbar);elsepainter->setPen(penspace);painter->drawLine(nXPixel, m_nStartingYPixel, nXPixel, m_nStartingYPixel + m_nPixelHeight);}// advance the starting positionm_nStartingXPixel += nTempWidth;}////oDC.SelectObject(hPenOld);//::DeleteObject(hPenBar);//::DeleteObject(hPenSpace);// detach from the device context//oDC.Detach();return; }//////////////////////////////////////////////////////////////////////////////////// // // Name: // RetrievePattern() // // Description: // retrieves the bar pattern for a given character // // Arguments: // char cInputCharacter - the input character to get the bar pattern for // // Return: // CString - the bar pattern for the input character // // Called by: // CRationalCodabar::DrawBitmap() // ////////////////////////////////////////////////////////////////////////////////////QString CCode39::RetrievePattern(char c) {QString csCharPattern;switch (c){case '1':csCharPattern = "wnnwnnnnwn";break;case '2':csCharPattern = "nnwwnnnnwn";break;case '3':csCharPattern = "wnwwnnnnnn";break;case '4':csCharPattern = "nnnwwnnnwn";break;case '5':csCharPattern = "wnnwwnnnnn";break;case '6':csCharPattern = "nnwwwnnnnn";break;case '7':csCharPattern = "nnnwnnwnwn";break;case '8':csCharPattern = "wnnwnnwnnn";break;case '9':csCharPattern = "nnwwnnwnnn";break;case '0':csCharPattern = "nnnwwnwnnn";break;case 'A':csCharPattern = "wnnnnwnnwn";break;case 'B':csCharPattern = "nnwnnwnnwn";break;case 'C':csCharPattern = "wnwnnwnnnn";break;case 'D':csCharPattern = "nnnnwwnnwn";break;case 'E':csCharPattern = "wnnnwwnnnn";break;case 'F':csCharPattern = "nnwnwwnnnn";break;case 'G':csCharPattern = "nnnnnwwnwn";break;case 'H':csCharPattern = "wnnnnwwnnn";break;case 'I':csCharPattern = "nnwnnwwnnn";break;case 'J':csCharPattern = "nnnnwwwnnn";break;case 'K':csCharPattern = "wnnnnnnwwn";break;case 'L':csCharPattern = "nnwnnnnwwn";break;case 'M':csCharPattern = "wnwnnnnwnn";break;case 'N':csCharPattern = "nnnnwnnwwn";break;case 'O':csCharPattern = "wnnnwnnwnn";break;case 'P':csCharPattern = "nnwnwnnwnn";break;case 'Q':csCharPattern = "nnnnnnwwwn";break;case 'R':csCharPattern = "wnnnnnwwnn";break;case 'S':csCharPattern = "nnwnnnwwnn";break;case 'T':csCharPattern = "nnnnwnwwnn";break;case 'U':csCharPattern = "wwnnnnnnwn";break;case 'V':csCharPattern = "nwwnnnnnwn";break;case 'W':csCharPattern = "wwwnnnnnnn";break;case 'X':csCharPattern = "nwnnwnnnwn";break;case 'Y':csCharPattern = "wwnnwnnnnn";break;case 'Z':csCharPattern = "nwwnwnnnnn";break;case '-':csCharPattern = "nwnnnnwnwn";break;case '.':csCharPattern = "wwnnnnwnnn";break;case ' ':csCharPattern = "nwwnnnwnnn";break;case '*':csCharPattern = "nwnnwnwnnn";break;case '$':csCharPattern = "nwnwnwnnnn";break;case '/':csCharPattern = "nwnwnnnwnn";break;case '+':csCharPattern = "nwnnnwnwnn";break;case '%':csCharPattern = "nnnwnwnwnn";break;}return csCharPattern; }//////////////////////////////////////////////////////////////////////////////////// // // Name: // BitmapToClipboard() // // Description: // puts the specified bitmap on the clipboard // // Arguments: // none // // Return: // void // // Called by: // public class interface - called by users of the class // //////////////////////////////////////////////////////////////////////////////////// #if 0 void CCode39::BitmapToClipboard() {CDC memDC;CBitmap oBitmap;memDC.CreateCompatibleDC(NULL);m_hDC = memDC.GetSafeHdc();// create compatible, correctly sized bitmapoBitmap.CreateCompatibleBitmap(&memDC,m_nFinalBarcodePixelWidth,m_nPixelHeight);// select our bitmap into the device contextCBitmap * oldbm = memDC.SelectObject(&oBitmap);// turn area white - stock black bitmap is selectedmemDC.BitBlt(0,0,m_nFinalBarcodePixelWidth,m_nPixelHeight,NULL,0,0,WHITENESS);// draw bitmap into memory device contextDrawBitmap();// put bitmap on clipboard::OpenClipboard(NULL);::EmptyClipboard();::SetClipboardData(CF_BITMAP, oBitmap.m_hObject);::CloseClipboard();// deselect object out of device contextmemDC.SelectObject(oldbm);// make sure bitmap not deleted with CBitmap object oBitmap.Detach();return; } #endifmytest.cpp中的調(diào)用
QPrintDialog dialog(&m_printer, this); if (dialog.exec() == QDialog::Accepted) {QPainter painter(&m_printer);CCode39 mycode;mycode.LoadData(QString("ST-1435887826818763"), QString("pos.jpg"), 1, 30, 5, 5, 3.0);mycode.DrawBitmap(&painter); }4.參考
Barcode.h Barcode.cpp Code39.h Code39.cpp是codeproject上的代碼用mfc寫(xiě)的 我改動(dòng)了下
總結(jié)
- 上一篇: Redis 队列好处
- 下一篇: 台达ES2与台达温控器ASCII通讯程序