将QQMllistProperty从QML传递到C 作为参数
Pass QQmlListProperty from QML to C++ as parameter
我有一个QML ProviderItem,该QML ProviderItem具有对象属性返回QObject衍生对象的列表。
我想将此列表传递给另一个QML ConsumerItem作为其功能消耗量的属性。问题在于,我总是会获得空的qqmllistProperty,所有回调函数设置为0,并且数据指针设置为0(我认为这些是默认结构的值)
类似的东西:
ProviderItem.h
class ProviderItem : public QObject, public QQmlParserStatus
{
Q_OBJECT
public:
Q_PROPERTY(QQmlListProperty<QObject> objects READ objects NOTIFY objectsChanged)
QQmlListProperty<QObject> objects();
static int objects_count(QQmlListProperty<QObject> *);
static QObject* objects_at(QQmlListProperty<QObject> *, int);
private:
QList<QObject*> m_objects;
}
provideritem.cpp
QQmlListProperty<QObject> ProviderItemPrivate::objects()
{
return QQmlListProperty<QObject>(this, nullptr,
ProviderItem::objects_count,
ProviderItem::objects_at);
}
QObject* ProviderItem::objects_at(QQmlListProperty<QObject> *prop, int index)
{
ProviderItem* provider = qobject_cast<ProviderItem*>(prop->object)
return provider->m_objects.at(index);
}
int ProviderItem::objects_count(QQmlListProperty<QObject> *prop)
{
ProviderItem* provider = qobject_cast<ProviderItem*>(prop->object)
return provider->m_objects.count();
}
commutitem.h
class ConsumerItem: public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void consumeAll(QQmlListProperty<QObject> obj);
};
commutitem.cpp
void ConsumerItem::consumeAll(QQmlListProperty<QObject> obj)
{
qDebug() << obj.count(); // thows exeption as count callback is 0
}
main.qml
Provider {
id: objectProvider
}
Consumer {
id: objectConsumer
}
Connections {
target: objectProvider
onObjectsChanged: {
console.debug(objectProvider.objects) // gives [object Object]
objectConsumer.consumeAll(objectProvider.objects)
var test = objectProvider.objects
console.debug(test) // gives [object Object]
Thermonav.testList(objectProvider.objects)
}
}
显然注册了ProviderEtem和ConsumerEtem:
main.cpp
qmlRegisterType<ProviderItem>(uri, major, minor, "Provider");
qmlRegisterType<ConsumerItem>(uri, major, minor, "Consumer");
我也尝试了:
Q_INVOKABLE void consumeAll(QVariantMap obj);
Q_INVOKABLE void consumeAll(QQmlListProperty<QObject> obj);
Q_INVOKABLE void consumeAll(void* p);
Q_INVOKABLE void consumeAll(QVariant p);
,但是每次我获得默认结构的值时。
根据本文:
与C 集成时,请注意,从C 传递到QML中的任何QQMLLISTPROPERTY值都会自动转换为列表值,而Vice-vices-vices-vices-vices。
因此,qml [对象对象]中的输出对我来说是合法的,因为"列表"不是JS数据类型。但它还说,QML列表应在qqmllistproperty中转换回绝对不适合我(或者我做错了)。
我正在使用QT 5.12.0
那么,如何将C 创建的QQMllistProperty传递给QML列表,然后转到C ?
如果使用QVariant
并打印:
class ConsumerItem: public QObject
{
Q_OBJECT
public:
using QObject::QObject;
Q_INVOKABLE void consumeAll(QVariant objects){
qDebug() << objects;
}
};
您得到:
QVariant(QQmlListReference, )
因此,解决方案是使用QQmlListReference
:
class ConsumerItem: public QObject
{
Q_OBJECT
public:
using QObject::QObject;
Q_INVOKABLE void consumeAll(const QQmlListReference & objects){
qDebug() << objects.count();
}
};
完成代码:
main.cpp
#include <QtQml>
#include <QtGui>
class Product: public QObject{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
public:
Product(const QString & name="", QObject* parent=nullptr):
QObject(parent), m_name(name){}
QString name() const{return m_name;}
void setName(const QString &name){
if(m_name == name) return;
m_name = name;
Q_EMIT nameChanged(m_name);
}
Q_SIGNAL void nameChanged(const QString &);
private:
QString m_name;
};
class ProviderItem: public QObject{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<Product> products READ products NOTIFY productsChanged)
public:
using QObject::QObject;
QQmlListProperty<Product> products(){
return QQmlListProperty<Product>(this, this,
&ProviderItem::appendProduct,
&ProviderItem::productCount,
&ProviderItem::product,
&ProviderItem::clearProducts);
}
void appendProduct(Product* p) {
m_products.append(p);
Q_EMIT productsChanged();
}
int productCount() const{return m_products.count();}
Product *product(int index) const{ return m_products.at(index);}
void clearProducts() {
m_products.clear();
Q_EMIT productsChanged();
}
Q_SIGNAL void productsChanged();
private:
static void appendProduct(QQmlListProperty<Product>* list, Product* p) {
reinterpret_cast<ProviderItem* >(list->data)->appendProduct(p);
}
static void clearProducts(QQmlListProperty<Product>* list) {
reinterpret_cast<ProviderItem* >(list->data)->clearProducts();
}
static Product* product(QQmlListProperty<Product>* list, int i) {
return reinterpret_cast<ProviderItem* >(list->data)->product(i);
}
static int productCount(QQmlListProperty<Product>* list) {
return reinterpret_cast<ProviderItem* >(list->data)->productCount();
}
QVector<Product *> m_products;
};
class ConsumerItem: public QObject
{
Q_OBJECT
public:
using QObject::QObject;
Q_INVOKABLE void consumeAll(const QQmlListReference & products){
for(int i=0; i<products.count(); ++i){
if(Product *product = qobject_cast<Product *>(products.at(i))){
qDebug()<< product->name();
}
}
}
};
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<Product>("foo", 1, 0, "Product");
qmlRegisterType<ProviderItem>("foo", 1, 0, "Provider");
qmlRegisterType<ConsumerItem>("foo", 1, 0, "Consumer");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
#include "main.moc"
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import foo 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
function create_product(){
var product = Qt.createQmlObject('import foo 1.0; Product {}',
provider,
"dynamicSnippet1");
product.name = "product"+provider.products.length;
provider.products.push(product)
}
Timer {
interval: 1000; running: true; repeat: true
onTriggered: create_product()
}
Provider{
id: provider
onProductsChanged: consumer.consumeAll(provider.products)
products: [
Product{name: "product0"},
Product{name: "product1"},
Product{name: "product2"}
]
}
Consumer{
id: consumer
}
}
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 如何将enable-if与模板参数和参数包一起使用