한국어
Qt
 

QAbstractItemModel은 C++로 구현된 모델을 QML에서 참조 할 수 있는 좋은 방법이다.  즉, QAbstractItemModel의 서브 클래스는 QML의 Model View (ListView, GridView, Repeater등과 같은) 에서 사용될 수 있다. C++ 데이터 세트 조작 방법은 성능에 큰 영향을 미치지 않고 QML UI 또는 뷰를 업데이트 할 수 있다.

 
QAbstractListModel은 추상 클래스이므로 인터페이스를 준수하도록 사용자가 구현해야한다. 다음과 같은 몇가지를 구현해줘야한다.
int rowCount();
QVariant data();
QHash<int, QByteArray> roleNames();
 
rowCount :
이 method는 단순히 데이터 세트이므로 m_data의 아이템 수를 반환한다.
int MyDataModel::rowCount(const QModelIndex &p) const
{
    Q_UNUSED(p)
    return m_data.size();
}
 
roleNames :
이 method는 사용자 정의 role enum을 key로 문자열 값을 매핑하고 매핑된 QHash를 리턴한다. roleNames() 메소드에 대한 호출이 될때 맵을 유지하기위해 정적 QHash 변수를 사용한다.
QHash<int, QByteArray> MyDataModel::roleNames() const
{
    static QHash<int, QByteArray> roles;
    roles[TitleRole] = "title";
    roles[ArtistNameRole] = "artistName";
    roles[DurationRole] = "duration";
    return roles;
}

 

data :
이 method는 모델을 참조하는 ListView가 개별 아이템 및 속성에 액세스 할 수있는 방법이다. 인수 index는 목록에있는 요소의 인덱스이고 role은 roleNames 메소드로 매핑된 열거형 값이다.
구현은 단순히 데이터를 저장하는 m_data 목록의 인덱스에서 항목을 반환하는 것이다.
QVariant MyDataModel::data(const QModelIndex &index, int role) const
{
    Q_UNUSED(role)

    QVariant value;

    switch(role)
    {
    case TitleRole:
        value = m_data[index.row()]->property("title");
        break;
    case ArtistNameRole:
        value = m_data[index.row()]->property("artistName");
        break;
    case DurationRole:
        value = m_data[index.row()]->property("duration");
        break;
    default:
        break;
    }

    return value;
}

 

추가로 이 모델에 count속성(Q_PROPERTY)을 추가한다.

Q_PROPERTY(int count READ count NOTIFY countChanged)

 

그리고 데이터를 세트를 조작 할 수 있는 다음과 같은 Method들도 필요 할 것이다.

void append(QObject *o);
void insert(QObject *o, int i);
void remove (int idx);

 

mymodel.h

#include <QAbstractListModel>

class MyDataModel : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
    enum ModelRoles {
        TitleRole = Qt::UserRole + 1,
        ArtistNameRole,
        DurationRole
    };

    explicit MyDataModel(QObject * parent = nullptr);

    int rowCount(const QModelIndex &p) const;
    QVariant data(const QModelIndex &index, int role) const;
    QHash<int, QByteArray> roleNames() const;

    int count() const;

public slots:
    void append(QObject *o);
    void insert(QObject *o, int i);
    void remove (int idx);

signals:
    void countChanged(int count);

private:
    QList<QObject*> m_data;
};

 

다음은 새로 서브클래싱한 모델의 사용밥법을 보여준다.

main.cpp

QQmlApplicationEngine engine;

// QAbstractListModel 서브클래싱 모델 객체 생성.
MyDataModel *model = new MyDataModel();

// 모델 아이템 생성.
QObject * item0 = new QObject();
item0->setProperty("title", "There's Nothing Holdin' Me Back");
item0->setProperty("artistName", "Shawn Mendes");

QObject * item1 = new QObject();
item1->setProperty("title", "Shape of You");
item1->setProperty("artistName", "Ed Sheeran");
//~ 모델 아이템 생성.

// 모델에 아이템 삽입.
model->append(item0);
model->append(item1);
//~ 모델에 아이템 삽입.

engine.rootContext()->setContextProperty("myModel", model);

 

main.qml

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    id: window
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ListView{
        id: list_
        anchors.fill: parent
        model: myModel

        delegate: Component{
            Text{
                anchors.horizontalCenter: parent.horizontalCenter
                height: 30
                font.bold: true
                text: title + " - " + artistName
            }
        }
    }
}

 

2019-09-08.png

번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 85854
118 최초의 Qt 6.0 스냅샷 제공 (First Qt 6.0 Snapshot Available) j2doll 2020.06.21 591
117 Qt MQTT 에 대해서 file makersweb 2020.06.02 959
116 Embedded Linux 에서 Qt 및 Graphics Stack file 운영자 2020.05.27 612
115 ShaderEffect QML Type을 이용한 버튼 클릭 효과 file makersweb 2020.05.22 1086
114 Qt기반의 서버와 클라이언트간 SOAP(Simple Object Access Protocol) file makersweb 2020.05.11 971
113 재진입(Reentrancy) 및 스레드 안전성(Thread-Safety) makersweb 2020.04.19 1216
112 Qt 5.15 및 Qt 6의 출시 일정 makersweb 2020.04.09 932
111 콘솔에서 사용자 입력받기 file makersweb 2020.03.22 51844
110 컨테이너 클래스 - QVector makersweb 2020.03.17 2835
109 Qt로 작성된 안드로이드 APP에 Splash Screen을 추가 file makersweb 2020.03.10 883
108 QLabel의 텍스트 색과 배경색을 변경 makersweb 2020.02.25 6605
107 라즈베리파이4에 대한 Qt 5.14.1 크로스컴파일 [1] file makersweb 2020.02.12 4449
106 QOpenGLWidget 을 투명하게 적용 file makersweb 2020.02.05 1031
105 2020년에 변경되는 Qt 오퍼 (Qt offering changes 2020) [2] j2doll 2020.01.31 720
104 Qt로 XML 파싱 : Qt 6에서 업데이트된 (Parsing XML with Qt: Updates for Qt 6) [1] j2doll 2020.01.16 979
103 Qt 멀티 스레드 프로그래밍 시 유의해야 할 몇 가지 makersweb 2020.01.13 4864
102 ApplicationWindow 와 메뉴바(MenuBar)구성 file makersweb 2020.01.04 1499
101 QThread 소개 및 예제 makersweb 2019.12.25 19441
100 Qt의 오픈소스 라이센스 소개 file makersweb 2019.12.15 12549
99 Qt for MCU 1.0 릴리즈 makersweb 2019.12.10 751