QT利用qrencode库生成二维码

0 537
索鸟 2020-08-11
需要:0索币

参考网上的教程,把代码下载下来,只保留.c和.h文件,删除qrencode.c文件,因为这是一个测试程序,里面已经包含了main函数。

新建一个QT工程,在工程目录下新建一个文件夹,把代码拷贝进去,然后再将所有的代码包含进来。

然后就可以在代码中调用了。

  1. #include "qrencode.h"
  2. QString strRocde = "hello";
  3. QRcode *m_qrcode = QRcode_encodeString(strRocde.toStdString().c_str(),
  4. 1,
  5. QR_ECLEVEL_H,
  6. QR_MODE_8,
  7. 1);
  8. //这是绘制函数
  9. void LogWidget::draw(QPainter &painter, int width, int height)
  10. {
  11. QColor foreground(Qt::black);
  12. painter.setBrush(foreground);
  13. const int qr_width = m_qrcode->width > 0 ? m_qrcode->width : 1;
  14. double scale_x = width / qr_width;
  15. double scale_y = height / qr_width;
  16. for( int y = 0; y < qr_width; y ++)
  17. {
  18. for(int x = 0; x < qr_width; x++)
  19. {
  20. unsigned char b = m_qrcode->data[y * qr_width + x];
  21. if(b & 0x01)
  22. {
  23. QRectF r(x * scale_x, y * scale_y, scale_x, scale_y);
  24. painter.drawRects(&r, 1);
  25. }
  26. }
  27. }
  28. if(1) //添加Logo的图片,绘制在二维码的中间bhavelogo
  29. {
  30. QPixmap picture(":/image/image/logoRitac.png");
  31. int logoWidth = width*0.28;
  32. int logoHeight = height * 0.28;
  33. painter.drawPixmap(width/2-logoWidth/2,height/2-logoHeight/2,logoWidth,logoHeight,picture);
  34. }
  35. }
  36. //重载的函数
  37. void LogWidget::paintEvent(QEvent *e)
  38. {
  39. QPainter painter(w);
  40. QColor background(Qt::white);
  41. painter.setBrush(background);
  42. painter.setPen(Qt::NoPen);
  43. int paintWidth = int(w->width()/m_qrcode->width)*m_qrcode->width ;
  44. int paintHeight = int(w->height()/m_qrcode->width)*m_qrcode->width;
  45. painter.drawRect(0, 0, paintWidth, paintHeight);
  46. if(m_qrcode != nullptr)
  47. {
  48. draw(painter, paintWidth,paintHeight);
  49. }
  50. }

在debug模式就可以正常使用了。。

 

但是坑爹的是,在release模式下编译的时候一大堆错误。晕死。

尝试了各种方法,都是无法解决。。最后试着用gcc编程库文件调用试试,发现是OK的。

编译方法:

d:\Qt\Qt5.9.7\Tools\mingw530_32\bin\mingw32-gcc.exe *.c -D HAVE_CONFIG_H -shared -o qrencode402.dll -Wl,--out-implib,qrencode402.lib

生成的文件

没有debug版本的,拷贝一个修改下名字就行了。。。以后移植到linux上,重新编译下库就行了。

然后包含外部库文件就行了。

回帖
  • 消灭零回复
相关主题
2020年最新最新Kubernetes视频教程(K8s)教程 2
程序员转型之制作网课变现,月入过万告别996 1
索鸟快传2.0发布啦 1
两个不同网络的电脑怎么实现文件的互相访问呢? 1
网盘多账号登录软件 1
Java实战闲云旅游项目基于vue+element-ui 1
单点登录技术解决方案基于OAuth2.0的网关鉴权RSA算法生成令牌 1
QT5获取剪贴板上文本信息QT设置剪贴板内容 1
springboot2实战在线购物系统电商系统 1
python web实战之爱家租房项目 1
windows COM实用入门教程 1
C++游戏开发之C++实现的水果忍者游戏 1
计算机视觉库opencv教程 1
node.js实战图书管理系统express框架实现 1
C++实战教程之远程桌面远程控制实战 1
相关主题
PHP7报A non well formed numeric value encountered 0
Linux系统下关闭mongodb的几种命令分享 0
mongodb删除数据、删除集合、删除数据库的命令 0
Git&Github极速入门与攻坚实战课程 0
python爬虫教程使用Django和scrapy实现 0
libnetsnmpmibs.so.31: cannot open shared object file 0
数据结构和算法视频教程 0
redis的hash结构怎么删除数据呢? 0
C++和LUA解析器的数据交互实战视频 0
mongodb errmsg" : "too many users are authenticated 0
C++基础入门视频教程 0
用30个小时精通C++视频教程可能吗? 0
C++分布式多线程游戏服务器开发视频教程socket tcp boost库 0
C++培训教程就业班教程 0
layui的util工具格式时间戳为字符串 0
C++实战教程之远程桌面远程控制实战 1
网络安全培训视频教程 0
LINUX_C++软件工程师视频教程高级项目实战 0
C++高级数据结构与算法视频教程 0
跨域问题很头疼?通过配置nginx轻松解决ajax跨域问题 0