1、github上下载(测试版本4.1.1)
2、将除了qrenc.c以外的所有.h和.c文件复制到项目中
3、添加预定义宏HAVE_CONFIG_H
4、新增config.h文件
5、包含qrencode.h调用QRcode_encodeString即可
#ifndef CONFIG_H
#define CONFIG_H
/* config.h. Generated from config.h.in by configure. */
/* config.h.in. Generated from configure.ac by autoheader. */
/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1
/* Define to 1 if using pthread is enabled. */
#undef HAVE_LIBPTHREAD
/* Define to 1 if you have the <memory.h> header file. */
#define HAVE_MEMORY_H 1
/* Define to 1 if you have the <stdint.h> header file. */
#define HAVE_STDINT_H 1
/* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1
/* Define to 1 if you have the <strings.h> header file. */
#define HAVE_STRINGS_H 1
/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1
/* Define to 1 if you have the `strdup' function. */
#define HAVE_STRDUP 1
/* Define to 1 if you have the <sys/stat.h> header file. */
#define HAVE_SYS_STAT_H 1
/* Define to 1 if you have the <sys/types.h> header file. */
#define HAVE_SYS_TYPES_H 1
/* Define to 1 if you have the <unistd.h> header file. */
#define HAVE_UNISTD_H 1
/* Major version number */
#define MAJOR_VERSION 4
/* Micro version number */
#define MICRO_VERSION 0
/* Minor version number */
#define MINOR_VERSION 0
/* Name of package */
#define PACKAGE "qrencode"
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT ""
/* Define to the full name of this package. */
#define PACKAGE_NAME "QRencode"
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "QRencode 4.0.0"
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "qrencode"
/* Define to the home page for this package. */
#define PACKAGE_URL ""
/* Define to the version of this package. */
#define PACKAGE_VERSION "4.0.0"
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1
/* Version number of package */
#define VERSION "4.0.0"
#define inline
/* Define to 'static' if no test programs will be compiled. */
#define STATIC_IN_RELEASE static
/* #undef WITH_TESTS */
#endif // CONFIG_H
QString qstr = ui->lineEdit->text();
QRcode* qrcode = QRcode_encodeString(qstr.toStdString().c_str(), 5, QR_ECLEVEL_H, QR_MODE_8, 1);
if (NULL == qrcode)
{
qDebug() << "QRcode_encodeString failed";
return;
}
if(qrcode->width <= 0)
{
return;
}
int scale = 8;
int width = qrcode->width * scale;
QImage img = QImage(width, width, QImage::Format_Mono);
QPainter painter(&img);
painter.fillRect(0, 0, width, width, Qt::white);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::black);
for (int y = 0; y < qrcode->width; y++)
{
for (int x = 0; x < qrcode->width; x++)
{
if (qrcode->data[y * qrcode->width + x] & 1)
{
painter.drawRect(QRect(x * scale, y * scale, scale, scale));
}
}
}
QRcode_free(qrcode);