我的问题是,如何在自定义对象中指定一个自定义对象,作为派生自该对象的角色,QAbstractListModel
以便在可视化它时ListView
可以访问其成员变量。这里有一个简单的代码示例:
这是我的类,代表我的自定义对象:
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:=>修复所需的副本构造函数
在我的下方添加Q_DECLARE_METATYPE时,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;\
编辑2:
因此,我添加了@Evgeny建议的所有必需功能。我的代码现在可以正确编译,但是在运行时出现qml错误:TypeError: Property 'getType' of object QVariant(MyCustomObject) is not a function
我已经Q_INVOKABLE
在getType()
方法前面添加了内容,并且还MyCustomObject
从派生了类public QObject
。我已经Q_DECLARE_METATYPE
在MyCustomObject
头文件的底部添加了。在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();
}
我最初发布的所有其他功能都相同。
首先,您需要为Qt元类型系统声明自定义对象。您应该Q_DECLARE_METATYPE
为此使用宏。另外,您可能需要使用qRegisterMetaType
功能。然后,您应该注册您的对象以将其与QML一起使用。您应该使用qmlRegisterType
函数。
还要确保您将其Q_INVOKABLE
用于对象方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句