-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added the QT image provider as a library
- Loading branch information
Showing
12 changed files
with
191 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@PACKAGE_INIT@ | ||
|
||
include ( "${CMAKE_CURRENT_LIST_DIR}/QrGenTargets.cmake" ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# QrGen | ||
|
||
The code is based on [QR Code generator library](https://github.com/nayuki/QR-Code-generator) and produce a library for the generation of a QR code of certain data. | ||
CMake produce the target 'QrGen' so one can link to this library like | ||
``` | ||
target_link_libraries(<target> <PRIVATE|PUBLIC|INTERFACE> QrGen) | ||
``` | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
cmake_minimum_required(VERSION 3.16) | ||
project(qmlqr LANGUAGES CXX) | ||
|
||
set(CMAKE_AUTOMOC ON) | ||
include(local_conf.cmake OPTIONAL) | ||
|
||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Quick Core5Compat Svg) | ||
|
||
qt_add_library(QtQrGen Qrimageprovider.cpp include/Qrimageprovider.hpp) | ||
|
||
set_target_properties(QtQrGen PROPERTIES | ||
WIN32_EXECUTABLE TRUE | ||
MACOSX_BUNDLE TRUE | ||
) | ||
target_include_directories(QtQrGen PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
"$<INSTALL_INTERFACE:include>") | ||
target_link_libraries(QtQrGen PUBLIC | ||
Qt6::Quick | ||
) | ||
target_link_libraries(QtQrGen PRIVATE | ||
QrGen | ||
Qt6::Core | ||
Qt6::Gui | ||
Qt6::Qml | ||
Qt6::Quick | ||
Qt6::Core5Compat | ||
Qt6::Svg | ||
) | ||
|
||
install(TARGETS QtQrGen EXPORT QtQrGenTargets LIBRARY DESTINATION lib) | ||
install(DIRECTORY include/ DESTINATION include/) | ||
|
||
install(EXPORT QtQrGenTargets | ||
FILE QtQrGenTargets.cmake | ||
NAMESPACE qr:: | ||
DESTINATION lib/cmake/QtQrGen | ||
) | ||
include(CMakePackageConfigHelpers) | ||
# generate the config file that is includes the exports | ||
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in | ||
"${CMAKE_CURRENT_BINARY_DIR}/QtQrGenConfig.cmake" | ||
INSTALL_DESTINATION "lib/cmake/QtQrGen" | ||
NO_SET_AND_CHECK_MACRO | ||
NO_CHECK_REQUIRED_COMPONENTS_MACRO | ||
) | ||
write_basic_package_version_file( | ||
"${CMAKE_CURRENT_BINARY_DIR}/QtQrGenConfigVersion.cmake" | ||
VERSION "0.1.1.0" | ||
COMPATIBILITY AnyNewerVersion | ||
) | ||
install(FILES | ||
${CMAKE_CURRENT_BINARY_DIR}/QtQrGenConfig.cmake | ||
${CMAKE_CURRENT_BINARY_DIR}/QtQrGenConfigVersion.cmake | ||
DESTINATION lib/cmake/QtQrGen | ||
) | ||
export(EXPORT QtQrGenTargets | ||
FILE "${CMAKE_CURRENT_BINARY_DIR}/QtQrGenTargets.cmake" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@PACKAGE_INIT@ | ||
|
||
include ( "${CMAKE_CURRENT_LIST_DIR}/QtQrGenTargets.cmake" ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <QPainter> | ||
#include <QStringRef> | ||
#include <QSvgRenderer> | ||
#include "qrcodegen.hpp" | ||
#include "Qrimageprovider.hpp" | ||
#include <QDebug> | ||
using namespace qrcodegen; | ||
|
||
QPixmap QRImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) | ||
{ | ||
const int width = 100; | ||
|
||
const auto max=(requestedSize.width()>requestedSize.height())?requestedSize.width():requestedSize.height(); | ||
|
||
QPixmap pixmap(max > 0 ? max : width, | ||
max> 0 ? max : width); | ||
|
||
*size = pixmap.size(); | ||
const QrCode qr = QrCode::encodeText(id.toStdString().c_str(), static_cast<QrCode::Ecc>(errC)); | ||
const auto qrSVGstr=toSvgString(qr, color.name().toStdString()); | ||
|
||
auto qrImage=QSvgRenderer(QByteArray::fromStdString(qrSVGstr)); | ||
QPainter Painter; | ||
|
||
Painter.begin(&pixmap); | ||
qrImage.render(&Painter); | ||
Painter.end(); | ||
return pixmap; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#QtQrGen | ||
|
||
This code produce a library with a custom ImageProvider of Qt. The image provider print a QRCODE from a string. | ||
CMake produce the target 'QtQrGen' so one can link to this library like | ||
``` | ||
target_link_libraries(<target> <PRIVATE|PUBLIC|INTERFACE> QtQrGen) | ||
``` | ||
|
||
|
||
## Showing the QRCODE on QML aplications will be simple as | ||
|
||
``` | ||
Image { | ||
sourceSize.width: 300 | ||
source: "image://qrcode/https://eddytheco.github.io/" | ||
} | ||
``` | ||
|
||
For this to work one has to add the custom ImageProvider to the QML engine like in the following main.cpp | ||
``` | ||
#include <QGuiApplication> | ||
#include <QQmlApplicationEngine> | ||
#include "Qrimageprovider.hpp" | ||
int main(int argc, char *argv[]) | ||
{ | ||
QGuiApplication app(argc, argv); | ||
QQmlApplicationEngine engine; | ||
engine.addImageProvider(QLatin1String("qrCode"), new QRImageProvider("blue",1)); | ||
const QUrl url(u"qrc:/app/main.qml"_qs); | ||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, | ||
&app, [url](QObject *obj, const QUrl &objUrl) { | ||
if (!obj && url == objUrl) | ||
QCoreApplication::exit(-1); | ||
}, Qt::QueuedConnection); | ||
engine.load(url); | ||
return app.exec(); | ||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <qquickimageprovider.h> | ||
|
||
|
||
|
||
|
||
class QRImageProvider : public QQuickImageProvider | ||
{ | ||
public: | ||
QRImageProvider(QColor col="black",int erc=0) | ||
: QQuickImageProvider(QQuickImageProvider::Pixmap),color(col),errC(erc) | ||
{ | ||
|
||
} | ||
|
||
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override; | ||
private: | ||
const QColor color; | ||
const int errC; | ||
}; | ||
|
||
|
||
|
This file was deleted.
Oops, something went wrong.