最近由于工作需要,经理要求写一个能生成QR二维码的上位机工具,配合产线生产用。
自已是一个上位机开发新手,对C++也不是太熟,QT工具也是现学现卖。在参考了网上的帖子后,结合这两天的实践,终于弄出来。现将过程记录一下,免得以后少走弯路。
按照网上的帖子,大多是要先生成 qrencode.lib库,然后在QT中进行调用。我在摸索过程中,虽然也成功的生成了这个库,但是在调用后还是出现各种问题,所以先放弃,待以后再进一步摸索。
1。在官网上下载libqrencode源代码。https://fukuchi.org/works/qrencode/
这里,我下的是最新版本qrencode-4.0.0.tar.gz并解压。
2。建立QT工程。
新建Qt Widgets Application工程,选择Dialog界面程序。
在工程目录下新建一个qrencode文件夹,用来放置qrencode库代码。从解压的qrencode-4.0.0文件夹中将其根目下的*.c *.h文件都copy到QT工程下的qrencode文件中,除了qrenc.c文件。
这里本来没有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 */
然后将该文件夹添加到QT工程中(在工程项目右键点击Add Existing Directory...)。
参照网上帖子,那些牛人对qrencode又做了进一步的封装,我就原样copy过来了,形成qrwidget.cpp 和qrwidget.h两文件。(参考http://blog.csdn.net/liyuanbhu/article/details/44599031,在这里一并感谢)
在.pro文件中添加如下一行:
DEFINES += HAVE_CONFIG_H
做UI界面。
注意中间是一个widget,需要将其提升为QRWidget。
最后实现save和 Generate两个button的槽函数,编译运行,最终GUI如下所示。
最后代码我也上传了,如有需要可以下载参考。点击打开链接