我的問題是,我如何指定一個自定義對象作爲從QAbstractListModel
派生的模型中的角色,因此當在ListView
內可視化時,我可以訪問其成員變量。到這裏有一個例子是一些簡單的代碼示例:如何將自定義對象定義爲QAbstractListModel中的角色?
這是我代表我的自定義對象類:的
class MyCustomObject {
public:
MyCustomObject(Qstring name, Qstring type);
QString getName();
QString getType();
private:
QString name;
QString type;
};
這是覆蓋data()
函數現在的樣子(但不工作),我MyModel
從QAbsractListModel
得出:
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:=>修復需要拷貝構造函數
當我在我的MyCustomObject
我收到以下錯誤添加Q_DECLARE_METATYPE:
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
我在getType()
方法前面加Q_INVOKABLE
,我也從public QObject
派生MyCustomObject
類。我在MyCustomObject
頭文件的底部添加了Q_DECLARE_METATYPE
。在MyCustomObject
構造我打電話qRegisterMetaType<MyCustomObject>("MyCustomObject")
,在我main
我也註冊類這樣qmlRegisterType<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()
功能看起來像現在我MyModel
來源於QAbsractListModel
:
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();
}
,我有p均其它功能osted原來是一樣的。
你會得到什麼錯誤信息? – sk2212