forked from manimanis/qrcodegen-qt6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
67 lines (55 loc) · 1.44 KB
/
Copy pathmainwindow.cpp
File metadata and controls
67 lines (55 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "mainwindow.h"
#include "qrcodegen.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QPixmap>
#include <QSvgRenderer>
#include <QFileDialog>
#include <QFile>
using namespace qrcodegen;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
std::string MainWindow::generateSVGFile(QString text)
{
QrCode qr0 = QrCode::encodeText(text.toStdString().c_str(), QrCode::Ecc::MEDIUM);
std::string svg = qrcodegen::toSvgString(qr0, 4);
return svg;
}
void MainWindow::renderSVGFile(std::string svgText)
{
QByteArray svgData(svgText.c_str());
QSvgRenderer renderer(svgData, this);
QPixmap pixmap(400, 400);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
renderer.render(&painter);
ui->lblSVG->setPixmap(pixmap);
}
void MainWindow::btnEncodeClicked()
{
m_svgText = generateSVGFile(ui->plainTextEdit->toPlainText());
renderSVGFile(m_svgText);
}
void MainWindow::btnSaveSVGFileClicked()
{
QString fileName = QFileDialog::getSaveFileName(
this,
tr("Save File"), QString(), tr("SVG Files (*.svg)")
);
if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream stream(&file);
stream << QString::fromStdString(m_svgText);
file.close();
}
}
}