ubuntu使用Qt生成二维码

3 篇文章 0 订阅
  1. 安装依赖库

sudo apt-get install libqrencode-dev libpng12-dev
  1. 在Qt的pro文件中添加动态库链接

LIBS += -lqrencode
  1. 上代码

头文件qrcodelabel.h

#ifndef QRCODELABEL_H
#define QRCODELABEL_H
 
#include <QLabel>
#include <QPainter>
#include <qrencode.h>
#include <QPaintEvent>
 
class QRcodeLabel : public QLabel
{
    Q_OBJECT
 
public:
    explicit QRcodeLabel(QWidget *parent = Q_NULLPTR);
    ~QRcodeLabel();
 
    void setString(const QString str);
    void setLogo(const QString path);
 
protected:
    virtual void paintEvent(QPaintEvent *ev);
 
private:
    QRcode* qrcode;
    QPixmap* image;
    bool logo;
 
};
 
#endif // QRCODELABEL_H

源文件qrcodelabel.cpp

#include "qrcodelabel.h"
 
QRcodeLabel::QRcodeLabel(QWidget *parent) :
    QLabel(parent)
{
    qrcode = NULL;
}
 
QRcodeLabel::~QRcodeLabel()
{
    if(qrcode != NULL) {
        QRcode_free(qrcode);
    }
}
 
void QRcodeLabel::setString(const QString str)
{
    if(qrcode != NULL) {
        QRcode_free(qrcode);
    }
    qrcode = QRcode_encodeString(str.toStdString().c_str(), 2, QR_ECLEVEL_H, QR_MODE_8, 1);
    update();
}
 
void QRcodeLabel::setLogo(const QString path)
{
    image = new QPixmap(path);
    logo = true;
    update();
}
 
void QRcodeLabel::paintEvent(QPaintEvent *ev)
{
    Q_UNUSED(ev);
    if(qrcode)
        return;

    int width = this->width();
    int height = this->height();
    int qrWidth = qrcode->width > 0 ? qrcode->width : 1 ;
    double scaleX = (double)width / (double)qrWidth ;
    double scaleY = (double)height / (double)qrWidth ;
 
    QPainter painter(this);
    painter.setBrush(Qt::white);
    painter.setPen(Qt::NoPen);
    painter.drawRect(this->rect());
    painter.setBrush(Qt::black);
    for(int y = 0; y < qrWidth; ++y) {
        for(int x = 0; x < qrWidth; ++x) {
            uchar node = qrcode->data[y*qrWidth + x];
            if(node & 0x01) {
                QRectF rectf(x*scaleX, y*scaleY, scaleX, scaleY);
                painter.drawRects(&rectf, 1);
            }
        }
    }
 
    if(logo) {
        int logoW = width * 0.2;
        int logoH = height * 0.2;
        QRect rect((width-logoW)/2, (height-logoH)/2, logoW, logoH);
        painter.setPen(QPen(Qt::red, 3));
        painter.drawRect(rect);
        painter.drawPixmap(rect, image->copy());
        logo = false;
    }
}

main.cpp

#include "qrcodelabel.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QRcodeLabel w;
    w.resize(200, 200);
    w.setString("12345678");
    w.show();
    return a.exec();
}
  1. 参考链接

https://www.freesion.com/article/5013334612/

https://www.pudn.com/detail/4853845

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值