0

QAbstractTableModelモデルを cpp で作成し、qml に接続しようとしています。

このコードはうまく機能します。

MyModel.h

#ifndef MYMODEL_H
#define MYMODEL_H

#include <QAbstractTableModel>

class MyModel : public QAbstractTableModel
{
    Q_OBJECT

public:
    enum AnimalRoles {
        TypeRole = Qt::UserRole + 1,
        SizeRole
    };

    explicit MyModel(QObject *parent = nullptr);

    // Basic functionality:
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

protected:
    QHash<int, QByteArray> roleNames() const;

private:
    QList<Animal> m_animals;
};

#endif // MYMODEL_H

MyModel.cpp

#include "MyModel.h"
#include <QDebug>

MyModel::MyModel(QObject *parent)
    : QAbstractTableModel(parent)
{
        qDebug() << __FUNCTION__;
        addAnimal(Animal("Wolf", "Medium"));
        addAnimal(Animal("Polar bear", "Large"));
        addAnimal(Animal("Quoll", "Small"));

}

int MyModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_animals.size();
}

int MyModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return 2;
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
    qDebug() << __FUNCTION__ << index.row() << index.column() << role;
    if (!index.isValid())
        return QVariant();
    const Animal &animal = m_animals[index.row()];
    switch (role) {
    case TypeRole:
        return animal.type();
    case SizeRole:
        return animal.size();
    default:
        break;
    }

    return QVariant();
}

void MyModel::addAnimal(const Animal &animal)
{
    qDebug() << __FUNCTION__;
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_animals << animal;
    endInsertRows();
}

QHash<int, QByteArray> MyModel::roleNames() const
{
    qDebug() << __FUNCTION__;
    QHash<int, QByteArray> roles;
    roles[TypeRole] = "type";
    roles[SizeRole] = "size";
    return roles;
}

main.cpp

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

        MyModel model;

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("myModel", &model);
    engine.load(QUrl(QStringLiteral("qrc:/resources/qmls/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

main_test.qml

import QtQuick 2.0
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    id: main_view
    width: 250
    height: 600
    visible: true

    ListView {
        id: list_view
        width: 200; height: 250

        model: myModel
        delegate: Text { text: "Animal Test: " + type + ", " + size }
    }


    TableView {
        id: table_view
        objectName: "tableView"
        width: 250; height: 250
        anchors.top:  list_view.bottom
        model: myModel

        TableViewColumn {
            id: type_col
            role: "type"
            title: "Type"
            width: 100
        }
        TableViewColumn {
            id: size_col
            role: "size"
            title: "Size"
            width: 100
        }
    }

}

このように見えます

ここに画像の説明を入力

しかし、main.cpp を少し変更すると、リスト ビューは通常どおり表示されますが、テーブル ビューは表示されません。

main.cpp

#include "MainView.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    MainView mainView;
    return app.exec();
}

MainView.h

#ifndef MAINVIEW_H
#define MAINVIEW_H

#include <QObject>
#include <QQmlApplicationEngine>

class MainView: public QObject
{
    Q_OBJECT
public:
   explicit  MainView(QObject *parent=nullptr);
    void initializeView();

private:
    QQmlApplicationEngine m_engine;
};

#endif // MAINVIEW_H

MainView.cpp

#include "MainView.h"
#include "MyModel.h"
#include <QQmlContext>

MainView::MainView(QObject *parent)
    : QObject(parent)
{
    initializeView();
}

void MainView::initializeView()
{
    MyModel model;
    m_engine.rootContext()->setContextProperty("myModel", &model);
    m_engine.load(QUrl(QStringLiteral("qrc:/resources/qmls/main_test.qml")));

}

このように見えます。

ここに画像の説明を入力

なぜこれが起こるのか本当にわかりません。2 番目のケースでは、ListView と TableView の違いは何ですか? そして、それを修正する方法、つまり2番目のケースでデータを表示する方法は? 少し早いですがお礼を。

4

1 に答える 1