Qml 如何在QAbstractListModel中将自定义对象定义为角色?
我的问题是,如何将自定义对象指定为从Qml 如何在QAbstractListModel中将自定义对象定义为角色?,qml,qt5,qabstractlistmodel,Qml,Qt5,Qabstractlistmodel,我的问题是,如何将自定义对象指定为从QAbstractListModel派生的模型中的角色,以便在ListView中可视化它时可以访问其成员变量。下面是一些简单的代码示例: 这是表示自定义对象的类: class MyCustomObject { public: MyCustomObject(Qstring name, Qstring type); QString getName(); QString getType(); private: QString
QAbstractListModel
派生的模型中的角色,以便在ListView
中可视化它时可以访问其成员变量。下面是一些简单的代码示例:
这是表示自定义对象的类:
class MyCustomObject {
public:
MyCustomObject(Qstring name, Qstring type);
QString getName();
QString getType();
private:
QString name;
QString type;
};
这就是从qabstractlistmodel
派生的myMyModel
被重写的data()
函数现在的样子(但它不工作):
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();
}
QHash<int, QByteArray> AtomModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[SomeRole] = "someRole";
roles[MyCustomRole] = "myCustomRole";
return roles;
}
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();
}
下面是我的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_元类型时,我收到以下错误:
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;\
这就是从qabstractlistmodel
派生的myMyModel
被重写的data()
函数现在的样子:
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();
}
QHash<int, QByteArray> AtomModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[SomeRole] = "someRole";
roles[MyCustomRole] = "myCustomRole";
return roles;
}
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();
}
QVariant MyModel::data(常量QModelIndex&index,int角色)常量{
if(index.row()<0 | | index.row()>m|u atoms.count()){
//如果(!index.isValid()){
返回QVariant();
}
const MyData&data=m_data[index.row()];
if(role==SomeRole){
返回数据。someString()
}
else if(角色==MyCustomRole){
QVariant var;//这是已更改的部件
var.setValue(data.myCustomObject);
收益var;
}
返回QVariant();
}
我最初发布的所有其他函数都是相同的。首先,您需要为Qt元类型系统声明自定义对象。您应该为此使用宏。您可能还需要使用函数。然后您应该注册您的对象以使用QML。您应该使用函数
另外,请确保使用对象方法。您会收到什么错误消息?我将尝试一下,然后返回结果!不幸的是,当我将
Q\u DECLARE\u元类型
放在我的MyCustomObject
下时,我收到一个错误消息:调用隐式删除了MyCustomObject
的复制构造函数。(有关更详细的错误消息,请参阅my EDIT1)也许您应该先阅读文档?>任何具有公共默认构造函数、公共副本构造函数和公共析构函数的类或结构都可以注册。没错,我的错误是……我确实阅读了文档,但我认为我有一个副本构造函数,因为我通常遵循三个规则,但我忘了为这个“虚拟”创建一个副本构造函数示例。感谢您指出!请尝试使用Q_属性
访问myCustomObject
方法。