gpt4 book ai didi

c++ - 遇到自定义 QObject 的 QVariantLists 和 QLists 的问题

转载 作者:行者123 更新时间:2023-11-28 06:38:23 25 4
gpt4 key购买 nike

我希望得到一些帮助,以解决我在个人项目中遇到的问题。本质上,我有一个自定义 QObject 继承者,它包含自己的各种数据元素,还有另一个自定义 QObject,我希望它有另一个自定义 QObject 的多个实例的一些集合元素。我已经尝试了多种实现方法,但到目前为止我还没有真正的成功,最接近的是使用 QVariantList。以下片段来 self 为重现问题而编写的示例,但它们遵循与更详细的对应部分相同的一般结构。

数据表示初始的自定义 QObject。

“数据.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

“数据.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 是将保存一些数据集合的对象

"测试容器.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

"测试容器.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();
}

"主.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
}
}

将所有这些放在一起为我提供了一个不错的选择:

“类型错误:对象 QVariant(Data) 的属性‘getName’不是函数”

想法?

最佳答案

经过数小时的研究和更多的反复试验,我终于找到了适合我的解决方案。而不是试图强制 QList<Object*>QVariantList为了正常运行,我设法让一切都与 QList<Data*> 一起工作和 QQmlListProperty<Data> .目前,我既可以将 QList 用作 TableView 之类的模型,也可以访问列表的各个元素。对于模型,它是这样的:

model: testContainer.theList

访问一个单独的元素是这样的:

text: theContainer.getData(0).name

以下是实现此目的的当前代码:

“数据.h”

#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

“数据.cpp”

#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;}

"测试容器.h"

#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

"测试容器.cpp"

#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);}

“ main.cpp ”

#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();
}

关于c++ - 遇到自定义 QObject 的 QVariantLists 和 QLists 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26398932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com