经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
qt生成二维码
来源:cnblogs  作者:徐沛东  时间:2018/10/20 15:34:02  对本文有异议

到官网下载qrencode

http://fukuchi.org/works/qrencode/index.html.en
qrenc.c不用,这个是测试用的,把config.h.in文件改为config.h文件,把.h文件和.cpp文件导入,在整个项目的pro文件中加入
DEFINES +=HAVE_CONFIG_H
OTHER_FILES += qrcode/config.h.in
  1. #include "qrcode/qrencode.h"
  2. void MainWindow::GenerateQRcode(QString tempstr)
  3. {
  4. QRcode *qrcode;
  5. qrcode=QRcode_encodeString(tempstr.toStdString().c_str(),2,QR_ECLEVEL_Q,QR_MODE_8,1);
  6. qint32 temp_width=ui->label->width();
  7. qint32 temp_height=ui->label->height();
  8. qDebug()<<"temp_width="<<temp_width<<";temp_height="<<temp_height;
  9. qint32 qrcode_width=qrcode->width>0?qrcode->width:1;
  10. double scale_x=(double)temp_width/(double)qrcode_width;
  11. double scale_y=(double)temp_height/(double)qrcode_width;
  12. QImage mainimg=QImage(temp_width,temp_height,QImage::Format_ARGB32);
  13. QPainter painter(&mainimg);
  14. QColor background(Qt::white);
  15. painter.setBrush(background);
  16. painter.setPen(Qt::NoPen);
  17. painter.drawRect(0,0,temp_width,temp_height);
  18. QColor foreground(Qt::black);
  19. painter.setBrush(foreground);
  20. for(qint32 y=0;y<qrcode_width;y++)
  21. {
  22. for(qint32 x=0;x<qrcode_width;x++)
  23. {
  24. unsigned char b=qrcode->data[y*qrcode_width+x];
  25. if(b &0x01)
  26. {
  27. QRectF r(x*scale_x,y*scale_y,scale_x,scale_y);
  28. painter.drawRects(&r,1);
  29. }
  30. }
  31. }
  32. QPixmap mainmap=QPixmap::fromImage(mainimg);
  33. QLabel *plabel = new QLabel();
  34. plabel->setPixmap(mainmap);
  35. plabel->setVisible(true);
  36. }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号