QT生成二维码
到官网下载qrencode
http://fukuchi.org/works/qrencode/index.html.en
qrenc.c不用,这个是测试用的,把config.h.in文件改为config.h文件,把.h文件和.cpp文件导入,在整个项目的pro文件中加入
config.h里
/* Major version number */ #undef MAJOR_VERSION #define MAJOR_VERSION 1 /* Micro version number */ #undef MICRO_VERSION #define MICRO_VERSION 1 /* Minor version number */ #undef MINOR_VERSION #define MINOR_VERSION 1 ...... /* Version number of package */ #undef VERSION #define VERSION 1
第二行的都是后加的
DEFINES +=HAVE_CONFIG_H
也可以直接用我打包的那几个文件: 见附件
qr.zip
dao
源代码:
#include "widget.h"
#include "ui_widget.h"
#include <qr/qrencode.h>
#include<QPainter>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QRcode *qrcode; //二维码数据
QString tempstr="http://jinesc.cn";
qrcode=QRcode_encodeString(tempstr.toStdString().c_str(),2,QR_ECLEVEL_Q,QR_MODE_8,1);
qrcode = QRcode_encodeString(tempstr.toStdString().c_str(), 2, QR_ECLEVEL_Q, QR_MODE_8, 1);
qint32 temp_width=ui->labQR->width(); //二维码图片的大小
qint32 temp_height=ui->labQR->height();
qint32 qrcode_width = qrcode->width > 0 ? qrcode->width : 1;
double scale_x = (double)temp_width / (double)qrcode_width; //二维码图片的缩放比例
double scale_y =(double) temp_height /(double) qrcode_width;
QImage mainimg=QImage(temp_width,temp_height,QImage::Format_ARGB32);
QPainter painter(&mainimg);
QColor background(Qt::white);
painter.setBrush(background);
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, temp_width, temp_height);
QColor foreground(Qt::black);
painter.setBrush(foreground);
for( qint32 y = 0; y < qrcode_width; y ++)
{
for(qint32 x = 0; x < qrcode_width; x++)
{
unsigned char b = qrcode->data[y * qrcode_width + x];
if(b & 0x01)
{
QRectF r(x * scale_x, y * scale_y, scale_x, scale_y);
painter.drawRects(&r, 1);
}
}
}
QPixmap mainmap=QPixmap::fromImage(mainimg);
ui->labQR->setPixmap(mainmap);
ui->labQR->setVisible(true);
}
Widget::~Widget()
{
delete ui;
}
登录后可发表评论
点击登录