2

何をすべきかまとめたいと思います。私はDataObjectメンバーを持つクラスを持っています:

QString first;QString last;QList<SubObject*> m_sublist; 

私はQAbstractListModelこれに慣れています。first と last は listview に参照できますが、 like は参照できませんm_sublist[0].lesson。次のようなエラーが表示されます。

未定義のプロパティ「レッスン」を読み取れません。

私のコード: dataobject.h

        class SubObject :public QObject
    {
        Q_OBJECT


    public:
        SubObject(const QString &lesson,QObject *parent = 0);
        const QString lesson;

    private:


    //    bool operator==(const SubObject*  &other) const {
    //           return other->lesson == lesson;
    //    }

    };

    class DataObject :public QObject{

        Q_OBJECT
    public:
    DataObject(const QString &firstName,
                const QString &lastName,
                const QList<SubObject*>   &sublist);


    QString first;
    QString last;
    QList<SubObject*> m_sublist;
    };

simplelistmodel.h

    class SimpleListModel : public QAbstractListModel {
        Q_OBJECT
    public:
        SimpleListModel(QObject *parent=0);
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
        int rowCount(const QModelIndex &parent = QModelIndex()) const;
        QHash<int,QByteArray> roleNames() const { return  m_roleNames; }



    private:
    // Q_DISABLE_COPY(SimpleListModel);

        QList<DataObject*> m_items;
        static const int FirstNameRole;
        static const int LastNameRole;
        static const int SubListRole;
    QHash<int, QByteArray> m_roleNames;
    };

シンプルリストモデル.cpp

        const int SimpleListModel::FirstNameRole = Qt::UserRole + 1;
    const int SimpleListModel::LastNameRole = Qt::UserRole + 2;
    const int SimpleListModel::SubListRole = Qt::UserRole + 3;


    SimpleListModel::SimpleListModel(QObject *parent) :
            QAbstractListModel(parent) {
        // Create dummy data for the list

        QList<SubObject*> mysublist;
        mysublist.append(new SubObject("MAT"));
        mysublist.append(new SubObject("FEN"));


        DataObject *first = new DataObject(QString("Arthur"), QString("Dent"),mysublist);
        DataObject *second = new DataObject(QString("Ford"), QString("Prefect"),mysublist);
        DataObject *third = new DataObject(QString("Zaphod"), QString("Beeblebrox"),mysublist);
        m_items.append(first);
        m_items.append(second);
        m_items.append(third);



    // m_roleNames = SimpleListModel::roleNames();
    m_roleNames.insert(FirstNameRole, QByteArray("firstName"));
    m_roleNames.insert(LastNameRole, QByteArray("lastName"));
    m_roleNames.insert(SubListRole, QByteArray("subList"));


    }

    int SimpleListModel::rowCount(const QModelIndex &) const {
    return m_items.size();
    }

    QVariant SimpleListModel::data(const QModelIndex &index,
                                                int role) const {
        if (!index.isValid())
            return QVariant(); // Return Null variant if index is invalid
        if (index.row() > (m_items.size()-1) )
            return QVariant();

        DataObject *dobj = m_items.at(index.row());
        switch (role) {
        case Qt::DisplayRole: // The default display role now displays the first name as well
        case FirstNameRole:
            return QVariant::fromValue(dobj->first);
        case LastNameRole:
            return QVariant::fromValue(dobj->last);
        case SubListRole:
            return QVariant::fromValue(dobj->m_sublist);

        default:
            return QVariant();
        }
    }

main.cpp

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

        QGuiApplication app(argc, argv);

        QQmlApplicationEngine engine;
        SimpleListModel model;


        QQmlContext *classContext = engine.rootContext();
        classContext->setContextProperty("absmodel",&model);

        engine.load(QUrl(QStringLiteral("qrc:/myuiscript.qml")));

        return app.exec(); }

myuiscript.qml

        import QtQuick 2.0
    import QtQuick.Window 2.0
    Window {
        id: bgRect
        width: 200
        height: 200
            color: "black"
            visible: true

            ListView {
                id: myListView
                anchors.fill: parent
                delegate: myDelegate
                model: absmodel

            }
            Component {
    id: myDelegate
            Item {
    width: 200
            height: 40
            Rectangle {
                anchors.fill: parent
                    anchors.margins: 2
                    radius: 5
                    color: "lightsteelblue"
                    Row {
                        anchors.verticalCenter: parent.verticalCenter
                            Text {

    text: firstName
            color: "black"
            font.bold: true
                            }
                        Text {
    text: subList[0].lesson
            color: "black"
                        }
                    }
            }
            }
            }


    }

解決策が見つかりません。仮想データ モデルは、単一タイプのオブジェクトを返します。FirsNameは文字列です。のように listview デリゲートを参照できませんfirstName(rolename)。のようにもLastName呼ばれlastName(rolename)ます。しかし、私はsubList(roleNames)のように参照することはできませんsublist[0].lesson

私の目標はとてもシンプルです。(int,QString ....)ロール名を使用して、単一の型をデリゲートのテキストに参照したい。を使用して、コレクションtype(QList<SubObject*>)をデリゲートのテキストに参照できませんrolename(subList[0].lesson)。それらを達成する方法は?

4

1 に答える 1

1

段階的に修正していきましょう。text: subList[0].lessonQMLのこの行はエラーメッセージを生成します

TypeError: undefined のプロパティ 'lesson' を読み取れません

つまりsubList[0]、 は未定義のオブジェクトであり、QML エンジンは を含むプロパティをlessonこのオブジェクトから読み取ることができません。実際、subListモデルから返されるオブジェクトは明確に定義されたQList<SubObject*>オブジェクトですが、QML リストではないsubList[0]ためそうではありません。リストを C++ から QMLQList<SubObject*>に正しく渡すには、 の代わりにa を返します。QVariantListQList

//class DataObject
DataObject(const QString &firstName,
            const QString &lastName,
            const QVariantList &sublist);
QVariantList    m_sublist; //use QVariantList instead of QList<SubObject*>

//---
//SimpleListModel::SimpleListModel
QVariantList mysublist; //use QVariantList instead of QList<SubObject*>
mysublist.append(QVariant::fromValue(new SubObject("MAT", this))); //remember parent
mysublist.append(QVariant::fromValue(new SubObject("FEN", this)));
//...

現在、subList[0]QML でアクセスできますが、subList[0].lesson. C++ クラスのプロパティにアクセスするには、プロパティをQ_PROPERTYマクロで明示的に定義します。

class SubObject :public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString lesson READ getLesson NOTIFY lessonChanged)

public:
    SubObject(const QString &lesson,QObject *parent = 0):
        QObject(parent), m_lesson(lesson){;}
    QString getLesson() const {return m_lesson;}

signals:
    void lessonChanged();

private:
    QString m_lesson;
};

QML コードが動作するようになりました。

于 2016-06-11T03:14:03.367 に答える