QT 调用qrencode4.0.2生成二维码

使用QT做一个小项目,需要使用到二维码功能。。网上搜了一些资料找到了 开源的qrencode。

下载地址:

https://fukuchi.org/works/qrencode/

我自己的:

https://pan.baidu.com/s/1TyDEa1gLrFoncTW_Z9fo6w

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

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

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

#include "qrencode.h"
    QString strRocde = "hello";
    QRcode *m_qrcode = QRcode_encodeString(strRocde.toStdString().c_str(),
                                   1,
                                   QR_ECLEVEL_H,
                                   QR_MODE_8,
                                   1);


//这是绘制函数
void LogWidget::draw(QPainter &painter, int width, int height)
{
    QColor foreground(Qt::black);
    painter.setBrush(foreground);
    const int qr_width = m_qrcode->width > 0 ? m_qrcode->width : 1;
    double scale_x = width / qr_width;
    double scale_y = height / qr_width;
    for( int y = 0; y < qr_width; y ++)
    {
        for(int x = 0; x < qr_width; x++)
        {
            unsigned char b = m_qrcode->data[y * qr_width + x];
            if(b & 0x01)
            {
                QRectF r(x * scale_x, y * scale_y, scale_x, scale_y);
                painter.drawRects(&r, 1);
            }
        }
    }

    if(1)     //添加Logo的图片,绘制在二维码的中间bhavelogo
    {
        QPixmap picture(":/image/image/logoRitac.png");
        int logoWidth = width*0.28;
        int logoHeight = height * 0.28;
        painter.drawPixmap(width/2-logoWidth/2,height/2-logoHeight/2,logoWidth,logoHeight,picture);
    }
}

//重载的函数
void LogWidget::paintEvent(QEvent *e)
{
    QPainter painter(w);
    QColor background(Qt::white);
    painter.setBrush(background);
    painter.setPen(Qt::NoPen);

    int paintWidth = int(w->width()/m_qrcode->width)*m_qrcode->width ;
    int paintHeight = int(w->height()/m_qrcode->width)*m_qrcode->width;

    painter.drawRect(0, 0, paintWidth, paintHeight);
    if(m_qrcode != nullptr)
    {
        draw(painter, paintWidth,paintHeight);
    }
}

在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上,重新编译下库就行了。

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值