Quantcast
Channel: How to draw a QR code with Qt in native C/C++ - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by MangoCat for How to draw a QR code with Qt in native C/C++

$
0
0

The following is Qt code I used to update a label (qrCode) on a form with the QR code for "text". The label is set to a fixed size (min and max width and height=256 in my case), and scaledContents true. You might do something more efficient than the RGB32 format, but it really shouldn't matter for occasional updates.

void MyClass::updateQrCode( QString text ){ using namespace qrcodegen;  // Create the QR Code object  QrCode qr = QrCode::encodeText( text.toUtf8().data(), QrCode::Ecc::MEDIUM );  qint32 sz = qr.getSize();  QImage im(sz,sz, QImage::Format_RGB32);    QRgb black = qRgb(  0,  0,  0);    QRgb white = qRgb(255,255,255);  for (int y = 0; y < sz; y++)    for (int x = 0; x < sz; x++)      im.setPixel(x,y,qr.getModule(x, y) ? black : white );  ui->qrCode->setPixmap( QPixmap::fromImage(im.scaled(256,256,Qt::KeepAspectRatio,Qt::FastTransformation),Qt::MonoOnly) );}

using the Nayuki library - just the QrCode.cpp and .hpp files from here: https://github.com/nayuki/QR-Code-generator/tree/master/cpp

incorporated in my project with this trivial .pri file (kept in the same folder as the QrCode files):

INCLUDEPATH += $$PWDDEPENDPATH  += $$PWDHEADERS += $$PWD/QrCode.hppSOURCES += $$PWD/QrCode.cpp

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>