Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to emit signal with variable as reference from qml to c++
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    econtech
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      melghawi
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved