QML and Qt Quick
2021.08.20 20:46

QML 에서 QR코드 생성

조회 수 5330 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

응용프로그램에서 QR코드를 생성하기 위한 다양한 오픈소스 라이브러리가 있다.

Qt응용프로그램에서도 QR코드 생성을 위해 사용 할 수 있는 많은 라이브러리가 있다. 그 중 레거시 JavaScript 라이브러리를 이용하여 QML수준에서 QR코드를 생성해본다.

사용하려는 라이브러리의 저장소는 다음과 같다.

https://github.com/kazuhikoarase/qrcode-generator/tree/master/js

이 라이브러리는 기본적으로 HTML에서 QR를 생성하기 위해 만들어 졌으며 qrcode.js 파일을 제공한다. HTML에서는 다음과 같이 사용된다.

<script type="text/javascript" src="qrcode.js"></script>
 
<div id="placeHolder"></div>
 
var typeNumber = 4;
var errorCorrectionLevel = 'L';
var qr = qrcode(typeNumber, errorCorrectionLevel);
qr.addData('Hi!');
qr.make();
document.getElementById('placeHolder').innerHTML = qr.createImgTag();

 

궁극적으로 이 라이브러리는 QR코드를 생성하고 img태그를 반환한다. 반환된 데이터를 div 요소에 삽입하여 화면에 출력한다.

그러면 이것을 우리의 Qt응용프로그램으로 가져오자. Qt 5.15 기준으로 Qt의 JavaScript 엔진은 ECMAScript 7을 지원하며 QML 의 Text 아이템은 HTML의 몇 가지 태그를 지원하므로 라이브러리를 그대로 사용할 수 있을 것이다. (지원되는 HTML 하위 집합)

실제로 사용 방법은 다음과 같이 매우 간단하다.

import QtQuick 2.12
import "qrcode.js" as QRCode

Window {
    visible: true
    width: 300
    height: 300
    title: qsTr("Hello World")

    Text {
        anchors.centerIn: parent
        font.pointSize: 14
        wrapMode: Text.WordWrap
        textFormat: Text.StyledText
        horizontalAlignment: parent.hAlign

        text: {
            var typeNumber = 0 //4
            var errorCorrectionLevel = 'L';
            var qr = QRCode.qrcode(typeNumber, errorCorrectionLevel);
            qr.addData('사용자가 제공하는 유니코드 텍스트');
            qr.make();
            return qr.createImgTag(3);
        }
    }
}

 

addData 메서드를 통해 텍스트를 전달한다. createImgTag의 반환값을 그대로 반환한다. 변환하려는 텍스트만 전달하면 된다.

앱을 실행하면 윈도우에 다음과 같이 QR코드가 그려진다. 스마트폰을 꺼내 카메라앱을 실행하고 아래 QR코드를 스캔해보자!

QRcode.png

TAG •

  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views131087
    read more
  2. No Image

    clazy 로 13개의 시그널, 슬롯 오류 해결

    Date2022.08.23 CategoryGeneral and Desktop Bymakersweb Views8525
    Read More
  3. No Image

    Qt 스마트 포인터 (QSharedPointer, QScopedPointer, QPointer)

    Date2022.08.18 CategoryC++ Class Bymakersweb Views5992
    Read More
  4. Qt 6.4에 추가될 Qt Quick 3D Physics

    Date2022.08.07 CategoryQt 6 Bymakersweb Views4515
    Read More
  5. No Image

    HTTPS URL을 연결할 때 SslHandshakeFailedError 오류

    Date2022.07.31 CategoryC++ Class Bymakersweb Views5367
    Read More
  6. No Image

    단일 인스턴스 Qt 응용 프로그램(Single-instance Application)

    Date2022.06.23 CategoryGeneral and Desktop Bymakersweb Views5971
    Read More
  7. Qt로 작성된 iOS 앱에서 시리얼 통신

    Date2022.04.30 CategoryMobile and Embedded Bymakersweb Views9547
    Read More
  8. No Image

    VirtualKeyboard 스타일 커스터 마이징

    Date2022.03.13 CategoryGeneral and Desktop Bymakersweb Views5762
    Read More
  9. No Image

    성능 고려 및 제안 사항

    Date2022.03.07 Bymakersweb Views4424
    Read More
  10. No Image

    Binding 타입으로 객체 속성 간 묶기

    Date2022.03.04 CategoryQML and Qt Quick Bymakersweb Views6714
    Read More
  11. No Image

    Qt Bluetooth Low Energy 개요

    Date2022.02.13 CategoryMobile and Embedded Bymakersweb Views5696
    Read More
  12. Qt Android 앱에 AdMob 배너달기

    Date2021.12.04 CategoryMobile and Embedded Bymakersweb Views3778
    Read More
  13. No Image

    Qt 6의 C++ 프로퍼티 바인딩 예제

    Date2021.11.01 CategoryQt 6 Bymakersweb Views6813
    Read More
  14. QML에서 앵커(anchors)로 위치 지정

    Date2021.10.05 CategoryQML and Qt Quick Bymakersweb Views10019
    Read More
  15. No Image

    안드로이드용 Qt 6.2

    Date2021.10.02 CategoryQt 6 Bymakersweb Views10110
    Read More
  16. Qt 응용프로그램에서 PDF 문서 렌더링

    Date2021.09.23 CategoryGeneral and Desktop Bymakersweb Views5623
    Read More
  17. QML에서 Websocket 서버와 통신

    Date2021.09.18 CategoryQML and Qt Quick Bymakersweb Views5379
    Read More
  18. No Image

    QML 코딩 규칙

    Date2021.09.05 CategoryQML and Qt Quick Bymakersweb Views9138
    Read More
  19. QML 에서 QR코드 생성

    Date2021.08.20 CategoryQML and Qt Quick Bymakersweb Views5330
    Read More
  20. No Image

    앱을 종료할 때 QML 바인딩 오류를 피하는 방법

    Date2021.08.08 CategoryQML and Qt Quick Bymakersweb Views9781
    Read More
  21. Qt 응용프로그램에서 Lottie Animation사용

    Date2021.05.30 CategoryQML and Qt Quick Bymakersweb Views5557
    Read More
Board Pagination Prev 1 2 3 4 5 9 Next
/ 9