当前位置:网站首页>QT | string generation QR code function
QT | string generation QR code function
2022-07-24 07:51:00 【InfoQ】

Function realization
development environment :WIN10 Environmental Science + VS2017 + Qt 5.14.2 64 Bit development environment
Resource package data

Configuration properties
1: Header file settings

2: Static library settings




QString qExePath = QCoreApplication::applicationDirPath();
QDir::setCurrent(qExePath);
QR code operation
1: establish QR Operation classes are used to draw QImage graphics
#include <QPainter>
#include <xstring>
class CQRCodeOperation
{
public:
CQRCodeOperation();
~CQRCodeOperation();
QImage GeneratedGraphics(std::string sData, QSize nsize); // Generate graphics
private:
QPixmap m_imgIcon;
};
2: Generate QR code graphics QImage
2.1: Definition QImage Objects store generated content
QImage image(nsize, QImage::Format_RGB32);
image.fill(QColor("#000000"));
2.2: Construct drawing pointer
QPainter painter(&image);
if (!painter.isActive())
{
return image;
}
2.3: Get... From string QRcode Class instance
QRcode *qrCode = QRcode_encodeString(sData.c_str(), 1, QR_ECLEVEL_L, QR_MODE_8, 1);
2.4: Set point brush and background brush
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: Drawing graphics
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: Release QRcode The pointer
QRcode_free(qrCode);
2.7: Add QR code graphics
painter.setRenderHint(QPainter::Antialiasing, true); // Anti-Aliasing
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: Finish drawing
painter.end();
3: QR code function call
CQRCodeOperation dlg;
QImage img = dlg.GeneratedGraphics(sText, QSize(250, 250));
ui.labPng->setPixmap(QPixmap::fromImage(img));
summary
边栏推荐
- Selenium basic knowledge automatic login QQ space
- Game three piece chess
- Hcip day 7
- Using bidirectional linked list to realize stack (c)
- Selenium basic knowledge debugging method
- Debug No3 multi texture overlay
- Digital twin demonstration project -- Talking about simple pendulum (3) solid model exploration
- Qt|字符串生成二维码功能
- Default risk early warning preliminary competition scheme of bond issuing enterprises [AI competition]
- hcip第八天笔记
猜你喜欢

Simple Gateway - intranet server safely obtains external network data

hcip第十三天笔记

*Code understanding * common function parsing in pytoch

Introduction to C language II. Functions

Introduction to C language

Using bidirectional linked list to realize stack (c)

One click Copy and import of web interface data into postman
![[hiflow] Tencent cloud hiflow scene connector realizes intelligent campus information management](/img/a9/7cdab9264902b1e2947a43463f6b32.png)
[hiflow] Tencent cloud hiflow scene connector realizes intelligent campus information management

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

简易网闸-内网服务器安全获取外网数据
随机推荐
*Code understanding * common function parsing in pytoch
Game three piece chess
Flinksql UDF custom data source
[sklearn] RF cross validation out of bag data parameter learning curve grid search
abstract class
XSS vulnerability learning
Use JMeter to analyze and test the lottery probability of the lottery interface
Qt|字符串生成二维码功能
Do you know the use of string?
Intelligent robots and intelligent systems (Professor Zheng Zheng of Dalian University of Technology) -- 2. Mobile Robot Perception
Selenium use
Multiple optimization methods print prime numbers between 100 and 200
生成模型与判别模型
Selenium basic knowledge automatic search
MySQL -- subquery scalar subquery
爬虫学习-概述
Collection of binary tree topics
Solve the problem that Anaconda navigator cannot be opened
MySQL --- 子查询 - 标量子查询
Introduction to C language I. branch and loop statements