How to emit signal with variable as reference from qml to c++



  • I want to emit signal with list of objects as a reference fro QML to c++ code. In c++ i am accessing the reference.

    I worked for long time but i am not able to get it.

    Is there any idea to achieve. Please give suggestion.

    Thanks in advance



  • Hi there.

    I came up with a quick example that does what you want but with strings instead. Should follow the same concept for objects.

    main.qml
    @import QtQuick 2.0

    Item {
    width: 800
    height: 600
    signal listSignal(var list)

    property variant stringList: ["one", "two", "three"]
    
    Timer {
        interval: 2000
        onTriggered: listSignal(stringList)
        running: true
    }
    

    }

    @

    main.cpp
    @#include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include "signalcatcher.h"

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

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/Testing/main.qml"&#41;);
    viewer.showExpanded();
    
    SignalCatcher signalCatcher(viewer.rootObject());
    
    return app.exec();
    

    }
    @

    signalcatcher.h
    @#ifndef SIGNALCATCHER_H
    #define SIGNALCATCHER_H

    #include <QObject>
    #include <QVariant>

    class QQuickItem;

    class SignalCatcher : public QObject
    {
    Q_OBJECT

    public:
    SignalCatcher(QQuickItem *rootItem, QObject *parent = NULL);

    private slots:
    void processList(const QVariant &var);
    };

    #endif // SIGNALCATCHER_H
    @

    signalcatcher.cpp
    @#include <QQuickItem>
    #include "signalcatcher.h"

    SignalCatcher::SignalCatcher(QQuickItem *rootItem, QObject *parent)
    : QObject(parent)
    {
    connect(rootItem, SIGNAL(listSignal(QVariant)), this, SLOT(processList(QVariant)));
    }

    void SignalCatcher::processList(const QVariant &var)
    {
    QStringList list = var.toStringList();
    for(int i = 0; i < list.size(); i++)
    {
    qDebug(list[i].toStdString().c_str());
    }
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.