Qt QR Code Generator is a simple C++ class that uses the qrcodegen library to generate QR codes from QStrings in Qt applications. It can also split large payloads into animated QR codes and export them as looping GIFs.
- Copy the Qt-QrCodeGenerator folder in your
libfolder. - Include the Qt-QrCodeGenerator project include (pri) file using the
include()qmake function. - Use the
QrCodeGeneratorclass in your code:
#include <QrCodeGenerator>
QrCodeGenerator generator;
QString data = "https://www.example.com";
QImage qrCodeImage = generator.generateQr(data);- That's all! Check the example project as a reference for your project if needed.
Here are the steps to run the example using cmake (specific steps for LINUX ONLY):
cd ./example
mkdir -p build
cd build
cmake ../
make
./qrexample # runs executableFor payloads too large to fit (or scan reliably) in a single QR code, the
library can split the data across several frames that are displayed in
sequence. The simplest way is to ask for an animated GIF and let QMovie
handle playback:
#include <QrCodeGenerator>
#include <QBuffer>
#include <QMovie>
QrCodeGenerator generator;
QByteArray data = ...; // large payload, QString also supported
// 512 bytes per frame, 250 ms per frame -> looping GIF89a
QByteArray gif = generator.generateGifQr(data, 512, 250);
auto *buffer = new QBuffer(this);
buffer->setData(gif);
buffer->open(QIODevice::ReadOnly);
auto *movie = new QMovie(buffer, "gif", this);
label->setMovie(movie);
movie->start();
// ...or save it to disk
QFile file("qr.gif");
file.open(QFile::WriteOnly);
file.write(gif);If you need access to the individual frames (custom rendering, your own
timer, etc.), use generateAnimatedQr() instead:
AnimatedQrCode animated = generator.generateAnimatedQr(data, 512, 250);
int count = animated.frameCount(); // number of frames
QImage frame = animated.frame(0); // frame images, all the same size
int delay = animated.frameDuration(); // suggested ms per frame
animated.saveGif("qr.gif"); // same GIF encoder as generateGifQr()Each frame is a QR code (binary mode) containing:
"{index}/{total}|" + chunk
indexis the 1-based frame number,totalthe number of frames.chunkis a contiguous slice of the original payload, at mostchunkSizebytes long. Concatenating the chunks of frames1..totalyields the original payload.
This is the same format used by AnimatedQRCodeReader, so frames produced by this library can be decoded by that reader. Error detection within each frame is provided by the QR code's own error correction; the header lets a reader detect missing or duplicated frames and reassemble out of order.
This project is licensed under the MIT License. See the LICENSE file for details.
