作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的问题是,如何将自定义对象指定为从 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
委托(delegate)中的成员变量:
ListView {
width: 400
height: 400
model: myModel
delegate: Text {
text: "Type: " + myCustomRole.getType() + ", Name: " + myCustomRole.getName() + ", some string: " someRole
}
}
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;\
TypeError: Property 'getType' of object QVariant(MyCustomObject) is not a function
Q_INVOKABLE
在
getType()
前面方法,我也推导出
MyCustomObject
来自
public QObject
的类(class).我已添加
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
为您的对象方法。
关于qml - 如何将自定义对象定义为 QAbstractListModel 中的角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35858332/
我是一名优秀的程序员,十分优秀!