4

私の質問は、派生モデルでカスタム オブジェクトを役割として指定するにはどうすればよいのでしょうQAbstractListModelListView。ここに例を示すには、いくつかの簡単なコード例を示します。

これは、私のカスタム オブジェクトを表すクラスです。

class MyCustomObject {
  public:
    MyCustomObject(Qstring name, Qstring type);
    QString getName();
    QString getType();

  private:
    QString name;
    QString type;
};

これは、派生元のオーバーライドされたdata()関数が現在どのように見えるか (ただし、機能していません) です。MyModelQAbsractListModel

QVariant MyModel::data(const QModelIndex &index, int role) const {
    if (index.row() < 0 || index.row() > m_atoms.count()) {
    //if (!index.isValid()) {
        return QVariant();
    }

    const MyData &data = m_data[index.row()];
    if(role == SomeRole) {
        return data.someString()
    }
    else if (role == MyCustomRole) {
        return data.myCustomObject; // How can I do this?
    }

    return QVariant();
}

ここでは、ロール名を次のように指定しますMyModel

QHash<int, QByteArray> AtomModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[SomeRole] = "someRole";
    roles[MyCustomRole] = "myCustomRole";

    return roles;
}

これは、デリゲートでメンバー変数にListViewアクセスする方法の例を使用して、QML コードでどのように見えるかです。MyCustomObject

ListView {
        width: 400
        height: 400

        model: myModel
        delegate: Text {
            text: "Type: " + myCustomRole.getType() + ", Name: " + myCustomRole.getName() + ", some string: " someRole

        }
    }

EDIT1: => 必要なコピー コンストラクターを修正

Q_DECLARE_METATYPE を my の下に追加するとMyCustomObject、次のエラーが表示されます。

call to implicitly-deleted copy constructor of `MyCustomObject`
in instantiation of member function 'QtMetaTypePrivate::QMetaTypeFunctionHelper<MyCustomObject, true>::Construct' requested here
in instantiation of function template specialization 'qRegisterNormalizedMetaType<MyCustomObject>' requested here QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Construct,
    return qRegisterNormalizedMetaType<T>(normalizedTypeName, dummy, defined);
in instantiation of function template specialization 'qRegisterMetaType<MyCustomObject>' requested here
Q_DECLARE_METATYPE(MyCustomObject)
expanded from macro 'Q_DECLARE_METATYPE'
#define Q_DECLARE_METATYPE(TYPE) Q_DECLARE_METATYPE_IMPL(TYPE)
expanded from macro 'Q_DECLARE_METATYPE_IMPL' 
      const int newId = qRegisterMetaType< TYPE >(#TYPE,
copy constructor of 'MyCustomObject' is implicitly deleted because base class 'QObject' has a deleted copy constructor
class MyCustomObject : public QObject
'QObject' has been explicitly marked deleted here Q_DISABLE_COPY(QObject)
expanded from macro 'Q_DISABLE_COPY'
       Class(const Class &) Q_DECL_EQ_DELETE;\

EDIT2:

そのため、@Evgeny が提案した必要な機能をすべて追加しました。私のコードはエラーなしでコンパイルされるようになりましたが、実行時に次のような qml エラーが発生します。 TypeError: Property 'getType' of object QVariant(MyCustomObject) is not a function

Q_INVOKABLEメソッドの前に追加し、からクラスをgetType()派生させました。ヘッダーファイルの下部に追加しました。I callのコンストラクターで、 my I でクラスを登録する場合も、このようにしますMyCustomObjectpublic QObjectQ_DECLARE_METATYPEMyCustomObjectMyCustomObjectqRegisterMetaType<MyCustomObject>("MyCustomObject")mainqmlRegisterType<MyCustomObject>("com.test.mycustomobject", 1, 0, "MyCustomObject")

クラスはMyCustomObject次のようになります。

class MyCustomObject : public QObject {
  public:
    MyCustomObject();
    MyCustomObject(Qstring name, Qstring type);
    MyCustomObject(const MyCustomObject& obj);
    ~MyCustomObject();
    Q_INVOKABLE QString getName();
    Q_INVOKABLE QString getType();

  private:
    QString name;
    QString type;
};
Q_DECLARE_METATYPE(MyCustomObject)

これは、オーバーライドされた関数が、派生data()元の現在のように見える方法です:MyModelQAbsractListModel

QVariant MyModel::data(const QModelIndex &index, int role) const {
        if (index.row() < 0 || index.row() > m_atoms.count()) {
        //if (!index.isValid()) {
            return QVariant();
        }

        const MyData &data = m_data[index.row()];
        if(role == SomeRole) {
            return data.someString()
        }
        else if (role == MyCustomRole) {
            QVariant var; // this is the part, which has changed
            var.setValue(data.myCustomObject);
            return var;
        }

        return QVariant();
    }

私が最初に投稿した他のすべての機能は同じです。

4

1 に答える 1