QML and Qt Quick
2021.02.10 22:10

C++로 작성한 클래스를 QML에서 생성

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

단축키

Prev이전 문서

Next다음 문서

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

단축키

Prev이전 문서

Next다음 문서

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

Qml과 C++로 구현하는 GUI어플리케이션, 두번째 글로 C++로 작성한 클래스를 QML에서 생성하고 사용하는 방법이다.

 

C++과 QML 을 통합하는 방법중 QQmlContext의 setContextProperty 라는 메서드를 사용하면 C++ 코드에서 이미 생성된 객체를 QML 컨텍스트에 등록하여 QML에서 C++ 클래스의 메서드 및 프로퍼티에 접근할 수 있음을 기억할 것이다. 이 경우 QQmlContext 에서 객체의 소유권을 갖지 않기 때문에 객체가 더 이상 사용되지 않을 때(프로그램 종료)에 객체를 파괴 해줘야 한다.

반면 qmlRegisterType 템플릿 함수는 QML 시스템에 C++ 로 작성된 유형을 등록하고 QML컨텍스트에서 직접 생성한다. 

 

File을 읽고 쓰는 간단한 C++클래스를 작성하고 qmlRegisterType 함수를 이용해 이 클래스를 등록하여 최종적으로 QML에서 파일을 읽고 쓰는 예제를 작성해 본다.

 

FileIO 클래스의 헤더 파일이다. FileIO 구현은 간단하다. read, write 메서스와 source, text 프로퍼티 가 있는 간단한 클래스다.

fileio.h

#include <QtCore>

class FileIO : public QObject
{
    Q_OBJECT
    Q_DISABLE_COPY(FileIO)
    Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
public:
    FileIO(QObject *parent = nullptr);
    ~FileIO() override;

    Q_INVOKABLE void read();
    Q_INVOKABLE void write();
    QUrl source() const;
    QString text() const;

public slots:
    void setSource(QUrl source);
    void setText(QString text);

signals:
    void sourceChanged(QUrl arg);
    void textChanged(QString arg);

private:
    QUrl m_source;
    QString m_text;
};

 

read 메서드는 읽기 모드에서 파일을 열고 텍스트 스트림을 사용하여 데이터를 읽는다. 텍스트가 변경되면 emit textChanged(m_text)를 사용하여 변경 사항을 외부에 알려야한다.

void FileIO::read()
{
    if(m_source.isEmpty()) {
        return;
    }
    QFile file(m_source.toLocalFile());
    if(!file.exists()) {
        qWarning() << "Does not exits: " << m_source.toLocalFile();
        return;
    }
    if(file.open(QIODevice::ReadOnly)) {
        QTextStream stream(&file);
        m_text = stream.readAll();
        emit textChanged(m_text);
    }
}

 

write 메서드는 동일한 작업을 수행하지만 파일을 쓰기 모드로 열고 스트림을 사용하여 내용을 쓴다.

void FileIO::write()
{
    if(m_source.isEmpty()) {
        return;
    }
    QFile file(m_source.toLocalFile());
    if(file.open(QIODevice::WriteOnly)) {
        QTextStream stream(&file);
        stream << m_text;
    }
}

 

이제 qmlRegisterType 템플릿 함수를 호출하여 작성한 클래스를 QML 컨텍스트에 등록한다. 이때 네임스페이스 문자열( "net.makersweb.file"), 메이져 버전, 마이너 버전 번호, 그리고 QML 타입 이름을 지정한다.

#include "fileio.h"

...
qmlRegisterType<FileIO>("net.makersweb.file", 1, 0, "FileIO");
...

 

QML에서 등록된 유형을 사용하기위해 import net.makersweb.file 1.0 를 포함한다. 다음은 FileIO 를 사용하는 간단한 QML 코드이다.

main.qml

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Dialogs 1.2

import net.makersweb.file 1.0

Window {
    id: window
    visible: true
    width: 640
    height: 480

    property FileIO backend: FileIO{}

    FileDialog{
        id: dialog

        onAccepted: {
            backend.setSource(window.title = dialog.fileUrl)
            backend.read()

            text.text = backend.text
        }
    }

    Column {
        anchors.fill: parent

        Row {
            id: buttons
            anchors.horizontalCenter: parent.horizontalCenter
            Button {
                id: open
                text: qsTr("Open")

                onClicked: {
                    dialog.open()
                }
            }
            Button {
                id: save
                text: qsTr("Save")
                onClicked: {
                    backend.text = text.text
                    backend.write()
                }
            }
        }

        ScrollView {
            id: view
            contentHeight: Window.height - buttons.height
            contentWidth: parent.width

            TextArea {
                id: text
                selectByMouse: true
            }
        }

    }
}

 

프로그램을 실행 후 Open 버튼을 클릭 후 텍스트 파일을 읽고, 써지는 것을 확인 할 수 있다.

fileio.png

 

본문에서 사용된 FileIO 클래스 출처: http://qmlbook.github.io/ch18-extensions/extensions.html#using-fileio


  1. No Image notice

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

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

    싱글 샷(Single-Shot) 시그널/슬롯 연결

    Date2021.05.12 CategoryQt 6 Bymakersweb Views7999
    Read More
  3. No Image

    응용프로그램 자동실행 설정 (on Windows)

    Date2021.05.08 CategoryC++ Class Bymakersweb Views5652
    Read More
  4. No Image

    Qt 6 에서 프로퍼티 바인딩

    Date2021.04.03 CategoryQt 6 Bymakersweb Views5427
    Read More
  5. No Image

    QML과 JavaScript 의 숫자 관련 내장된 함수

    Date2021.03.28 CategoryQML and Qt Quick Bymakersweb Views7256
    Read More
  6. Qt 5 코드를 Qt 6로 포팅하기 위해 도움이 되는 Clazy Framework

    Date2021.03.01 CategoryQt 6 Bymakersweb Views6283
    Read More
  7. C++로 작성한 클래스를 QML에서 생성

    Date2021.02.10 CategoryQML and Qt Quick Bymakersweb Views10823
    Read More
  8. Qt MQTT의 pus/sub 튜토리얼

    Date2021.02.06 CategoryMobile and Embedded Bymakersweb Views7828
    Read More
  9. Qt 를 사용하거나 기반으로 하는 응용프로그램

    Date2021.01.30 CategoryGeneral and Desktop Bymakersweb Views10695
    Read More
  10. No Image

    Loader를 사용하여 동적으로 QML 로드

    Date2021.01.19 CategoryQML and Qt Quick Bymakersweb Views8584
    Read More
  11. No Image

    QThread 및 QMutex 예제

    Date2021.01.12 CategoryC++ Class Bymakersweb Views7616
    Read More
  12. No Image

    그래픽 소프트웨어에서 디자인 내보내기 (Exporting Designs from Graphics Software)

    Date2020.12.25 CategoryQML and Qt Quick Byj2doll Views6872
    Read More
  13. No Image

    Qt5Compat 라이브러리를 사용하여 Qt5에서 Qt6로 포팅

    Date2020.12.05 CategoryQt 6 Bymakersweb Views5751
    Read More
  14. Qt Quick Controls 2에 네이티브 데스크탑 스타일 추가

    Date2020.11.23 CategoryQt 6 Bymakersweb Views8738
    Read More
  15. No Image

    QML 바인딩 끊김 진단

    Date2020.11.08 CategoryQML and Qt Quick Bymakersweb Views6188
    Read More
  16. No Image

    QML과 코루틴(Coroutines)

    Date2020.11.03 CategoryQML and Qt Quick Bymakersweb Views6340
    Read More
  17. No Image

    Qt 6의 비동기 API

    Date2020.10.19 CategoryQt 6 Bymakersweb Views5914
    Read More
  18. No Image

    QRandomGenerator 클래스를 사용하여 난수(random values) 생성

    Date2020.10.17 CategoryC++ Class Bymakersweb Views7426
    Read More
  19. No Image

    Qt 6에서 QList 변경사항

    Date2020.10.08 CategoryQt 6 Bymakersweb Views6027
    Read More
  20. No Image

    Qt 6.0의 개발 호스트 및 대상 플랫폼

    Date2020.09.16 CategoryQt 6 Bymakersweb Views10033
    Read More
  21. No Image

    main함수 명령줄 옵션 해석

    Date2020.09.01 CategoryC++ Class Bymakersweb Views9117
    Read More
Board Pagination Prev 1 2 3 4 5 9 Next
/ 9