gpt4 book ai didi

c++ - 将 QVariant 从 QML 传递到 C++

转载 作者:行者123 更新时间:2023-11-28 05:37:10 31 4
gpt4 key购买 nike

我正在尝试将列表从 QML 发送到 C++。我尝试使用字符串和整数成功,但是当我尝试使用 QVariant 时出现错误:

QObject::connect: No such slot MyClass::cppSlot(QVariant) in ../test_2206/main.cpp:31

我的main.qml

import QtQuick 2.4
import QtQuick.Layouts 1.1
import Material.ListItems 0.1 as ListItem
import Material.Extras 0.1
import QtQuick.Controls 1.3 as QuickControls
import QtQuick.Controls 1.4
import Material 0.2


Window {
visible: true
property var listCloud: ["nuage1", "nuage2", "nuage3", "nuage4"]
id: item
width: 100; height: 100

signal qmlSignal(variant msg)

/* MouseArea {
anchors.fill: parent
onClicked: item.qmlSignal("Hello from QML")
}*/
Rectangle{
id:sousRect
color:"red"
width:100; height:100
Button{
id:buttonTest
onClicked: {
item.qmlSignal(listCloud)
}
}
}

}

我的 main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlProperty>
#include <QQmlComponent>
#include <QDebug>
#include "myclass.h"

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;

QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
QObject *object = component.create();

MyClass myClass;
QObject::connect(object, SIGNAL(qmlSignal(QVariant)),
&myClass, SLOT(cppSlot(QVariant)));


return app.exec();
}

我的类.h

#ifndef MYCLASS_H
#define MYCLASS_H
#include <QDebug>
#include <QString>
#include <QList>



class MyClass : public QObject
{
Q_OBJECT
public:
MyClass(QObject *parent = 0);
signals:
public slots:
void cppSlot(QVariant &msg);
};

#endif // MYCLASS_H

我的类.cpp

#include "myclass.h"

MyClass::MyClass(QObject *parent):
QObject(parent)
{

}
void MyClass::cppSlot(QVariant &msg){
qDebug() << "c++: bien reçu chef " << msg;
}

我不明白为什么我不能在这个信号中加入 QVariant 参数。欢迎任何帮助:)

最佳答案

更改您的插槽以按值而不是按引用接收他的参数。像这样:

    void cppSlot(QVariant msg);

我认为这与 QML 拒绝通过引用传递 Window 的属性有关。无论如何,Qt 经常发送信号按值,即使您的信号/槽签名[s]声明参数按引用。有关该主题的更多信息,请参阅 thisthat

关于c++ - 将 QVariant 从 QML 传递到 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37985201/

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