我希望得到一些帮助,因为我遇到了个人项目。基本上我有一个自定义QObject继承器,它包含自己的各种数据元素,另一个自定义QObject,我希望有一些其他自定义QObject的多个实例的集合元素。我已经尝试了各种方法来实现这一点,到目前为止我没有真正成功,最接近QVariantList。以下片段来自我写的一个例子,用于重新创建问题,但它们遵循与更详细的对应物相同的一般结构。
数据代表初始自定义QObject。
“data.h”
#ifndef DATA_H
#define DATA_H
#include <QObject>
class Data : public QObject
{
Q_OBJECT
public:
explicit Data(QObject *parent = 0);
Data(const Data &other);
~Data();
QString getName();
QString getContent();
void setName(QString newName);
void setContent(QString newContent);
private:
QString name, content;
};
Q_DECLARE_METATYPE(Data)
#endif // DATA_H
“data.cpp”
#include "data.h"
Data::Data(QObject *parent) :
QObject(parent)
{
name = "testData";
content = "testContent";
}
Data::Data(const Data &other){}
Data::~Data(){}
QString Data::getName(){return name;}
QString Data::getContent(){return content;}
void Data::setName(QString newName){name = newName;}
void Data::setContent(QString newContent){content = newContent;}
TestContainer是将保存一些数据集合的对象
“testcontainer.h”
#ifndef TESTCONTAINTER_H
#define TESTCONTAINER_H
#include <QObject>
#include <QString>
#include <QVariantList>
#include "data.h"
class TestContainer : public QObject
{
Q_OBJECT
public:
explicit TestContainer(QObject *parent = 0);
TestContainer(const TestContainer &other);
Q_INVOKABLE QVariantList getList();
private:
QString name;
QVariantList theList;
};
Q_DECLARE_METATYPE(TestContainer)
#endif // TESTCONTAINTER_H
“testcontainer.cpp”
#include "testcontainer.h"
TestContainer::TestContainer(QObject *parent) :
QObject(parent)
{
name = "test container";
Data temp;
theList.append(QVariant::fromValue(temp));
}
TestContainer::TestContainer(const TestContainer &other){
name = other.name;
theList = other.theList;
}
QVariantList TestContainer::getList(){return theList;}
“的main.cpp”
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "testcontainer.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qRegisterMetaType<TestContainer>("TestContainer");
QQmlApplicationEngine engine;
TestContainer theContainer;
engine.rootContext()->setContextProperty("theContainer", &theContainer);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
“main.qml”
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
visible: true
width: 320
height: 240
title: qsTr("Testing Functionality")
Text {
text: theContainer.getList()[0].getName();
anchors.centerIn: parent
}
}
把所有这些放在一起为我提供了一个很好的:
“TypeError:对象的属性'getName'QVariant(数据)不是函数”
思想?
答案 0 :(得分:1)
经过数小时的研究和更多的反复试验,我终于发现了一个适合我的解决方案。我没有尝试强制QList<Object*>
或QVariantList
正常运行,而是设法使所有内容与QList<Data*>
和QQmlListProperty<Data>
一致。目前我能够将QList用作TableView之类的模型,并且还能够访问列表中的各个元素。对于模型,它类似于:
model: testContainer.theList
要访问单个元素,例如:
text: theContainer.getData(0).name
以下是实现此目的的当前代码:
&#34; data.h&#34;
#ifndef DATA_H
#define DATA_H
#include <QObject>
class Data : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ getName WRITE setName)
Q_PROPERTY(QString content READ getContent WRITE setContent)
public:
explicit Data(QObject *parent = 0);
QString getName();
QString getContent();
void setName(QString newName);
void setContent(QString newContent);
private:
QString name, content;
};
#endif // DATA_H
&#34; data.cpp&#34;
#include "data.h"
Data::Data(QObject *parent) :
QObject(parent){}
QString Data::getName(){return name;}
QString Data::getContent(){return content;}
void Data::setName(QString newName){name = newName;}
void Data::setContent(QString newContent){content = newContent;}
&#34; testContainer.h&#34;
#ifndef TESTCONTAINTER_H
#define TESTCONTAINER_H
#include <QObject>
#include <QList>
#include <QQmlListProperty>
#include "data.h"
class TestContainer : public QObject
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<Data> theList READ getList)
public:
explicit TestContainer(QObject *parent = 0);
QQmlListProperty<Data> getList();
void addData(Data* d);
Q_INVOKABLE Data* getData(int i);
private:
QString name;
QList<Data*> theList;
};
#endif // TESTCONTAINTER_H
&#34; testContainer.cpp&#34;
#include "testcontainer.h"
TestContainer::TestContainer(QObject *parent) :
QObject(parent){}
QQmlListProperty<Data> TestContainer::getList(){
return QQmlListProperty<Data>(this, theList);}
void TestContainer::addData(Data* d){theList.append(d);}
Data* TestContainer::getData(int i){return theList.at(i);}
&#34;的main.cpp&#34;
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTQml>
#include "testcontainer.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<Data>("Custom", 1, 0, "Data");
qmlRegisterType<TestContainer>("Custom", 1, 0, "TestContainer");
QQmlApplicationEngine engine;
TestContainer theContainer;
Data d1;
d1.setName("test name 1");
d1.setContent("test content 1");
Data d2;
d2.setName("test name 2");
d2.setContent("test content 2");
theContainer.addData(&d1);
theContainer.addData(&d2);
engine.rootContext()->setContextProperty("theContainer", &theContainer);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}