当前位置:网站首页>Qt|字符串生成二维码功能
Qt|字符串生成二维码功能
2022-07-24 07:50:00 【InfoQ】

功能实现
开发环境:WIN10环境 + VS2017 + Qt 5.14.2 64位开发环境
资源包数据

配置属性
1:头文件设置

2:静态库设置




QString qExePath = QCoreApplication::applicationDirPath();
QDir::setCurrent(qExePath);
二维码操作
1:创建QR操作类用于绘制QImage图形
#include <QPainter>
#include <xstring>
class CQRCodeOperation
{
public:
CQRCodeOperation();
~CQRCodeOperation();
QImage GeneratedGraphics(std::string sData, QSize nsize); //生成图形
private:
QPixmap m_imgIcon;
};
2:生成二维码图形QImage
2.1:定义QImage对象存储生成内容
QImage image(nsize, QImage::Format_RGB32);
image.fill(QColor("#000000"));
2.2:构造绘图指针
QPainter painter(&image);
if (!painter.isActive())
{
return image;
}
2.3:根据字符串获取QRcode类实例
QRcode *qrCode = QRcode_encodeString(sData.c_str(), 1, QR_ECLEVEL_L, QR_MODE_8, 1);
2.4:设置点画刷以及背景画刷
QColor colorForPoint("#FFB6C1");
QColor colorForBackground("#ffffff");
painter.setBrush(colorForBackground);
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, image.width(), image.height());
painter.setBrush(colorForPoint);
2.5:绘制图形
const double &&s = (qrCode->width > 0) ? (qrCode->width) : (1);
const double &&aspect = image.width() / image.height();
const double &&scale = ((aspect > 1.0) ? image.height() : image.width()) / s;
for (int y = 0; y < s; ++y)
{
const int &&yy = static_cast<int>(y * s);
for (int x = 0; x < s; ++x)
{
const int &&xx = yy + x;
const unsigned char &b = qrCode->data[xx];
if (b & 0x01)
{
const double rx1 = x * scale, ry1 = y * scale;
QRectF r(rx1, ry1, scale, scale);
painter.drawRects(&r, 1);
}
}
}
2.6:释放QRcode指针
QRcode_free(qrCode);
2.7:添加二维码图形
painter.setRenderHint(QPainter::Antialiasing, true); //抗锯齿
int nLeft = (nsize.width() - 30) / 2;
int nTop = (nsize.height() - 30) / 2;
QRect rectPng(nLeft, nTop,30,30);
painter.drawPixmap(rectPng, m_imgIcon);
2.8:结束绘制
painter.end();
3:二维码函数调用
CQRCodeOperation dlg;
QImage img = dlg.GeneratedGraphics(sText, QSize(250, 250));
ui.labPng->setPixmap(QPixmap::fromImage(img));
总结
边栏推荐
- Selenium basic knowledge automatically login Baidu Post Bar
- Implement a queue with two stacks.
- Multiple optimization methods print prime numbers between 100 and 200
- Debug No4 use renderdoc to troubleshoot bugs
- MySQL --- 子查询 - 标量子查询
- 【sklearn】tree.DecisionTreeClassifier
- [sklearn] data preprocessing
- C language to achieve mine sweeping game
- Install librosa using Tsinghua image
- Log in to the server using the fortress machine (springboard machine)
猜你喜欢

无法自动装配,未找到“RedisTemplate类型的Bean

About the solution of thinking that you download torch as a GPU version, but the result is really a CPU version

Requests crawler multi page crawling to KFC restaurant location

hcip第八天笔记

Kubernetes: (I) basic concepts
![2022-07-23: given n items, each item has weight (w[i]) and value (v[i]), only two items can be selected at most, and the weight does not exceed bag. What is the maximum return value? N <= 10^5, w[i] <](/img/f4/ba2706e93f042dd8b110fac0d873c8.png)
2022-07-23: given n items, each item has weight (w[i]) and value (v[i]), only two items can be selected at most, and the weight does not exceed bag. What is the maximum return value? N <= 10^5, w[i] <

Debug No1 summarizes common solutions to bugs

GBK code in idea is converted to UTF-8 format ctrl+c+v one second solution perfect solution for single code file escape

About how to set colored fonts on the terminal

Appium doctor command error pit - resolved
随机推荐
Selenium basic knowledge automatic search
MySQL update uses case when to update the value of another field according to the value of one field
Do you know the use of string?
Introduction to C language III Array 4. Operators
Kubernetes:(一)基本概念
Service Vulnerability & FTP & RDP & SSH & Rsync
Multiple optimization methods print prime numbers between 100 and 200
Simple Gateway - intranet server safely obtains external network data
MySQL -- subquery scalar subquery
The difference between session and cookie
Reptile learning - Overview
MS SQL Server 2019 学习
JS_ Realize the separation of multiple lines of text into an array according to the newline
Amber tutorial A17 learning - concept
Automatic test and manual test
Facing Tencent (actual combat) - Test Development - detailed explanation of interns (face experience)
Fopen, fwrite, fseek, fTell, FREAD use demo
Train-clean-100 dataset
Use of ArrayList
Digital twin demonstration project -- Talking about simple pendulum (3) solid model exploration