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 send an array of integers from QML to C++? Can anyone fix this code such that ...
Forum Updated to NodeBB v4.3 + New Features

How to send an array of integers from QML to C++? Can anyone fix this code such that ...

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 2.7k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    mshefiti
    wrote on last edited by
    #1

    So I have a signal:

    QML:
    @signal qmlSendWorkflowIdsArraySignal(variant workflowIdsArray)@

    JS:
    @sendToCppWorkflowIdsArray(arrayOfWorkflowIds);@

    C++:
    @QObject::connect(qmlMainContentItemObject, SIGNAL(qmlSendWorkflowIdsArraySignal(QVariant)), &myController, SLOT(qmlToCppWorkflowIdsArraySlot(QVariant)));@

    then
    @void qmlToCppWorkflowIdsArraySlot(QVariant workflowIdsArray)
    {
    qDebug() << " +++++++++++++++ Cpp Array slot has been called: "<<workflowIdsArray;
    }@

    *Output: *
    @+++++++++++++++ Cpp Array slot has been called:
    QVariant(QVariantList, (QVariant(QString, "2") , QVariant(QString, "3") , QVariant(QString, "4") ) )@

    • How do I read this QVariantList inside QVariant, ie, lets say I want to read this first element of the array, that is QVariant(QString, "2"), then the other and the other. This looks very complicated like this.

    • All I need is to pass an array of integers from QML to C++, so I can read this array.

    • I dont want to use QVariant or a QVariantList, unless I have to as I see no other way to do it.

    Anyone any suggestion? Any professionals out there??

    Thank you !!!!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sohail
      wrote on last edited by
      #2

      I'm not sure I understand your question. I'd just use a QVariantList in your slot.

      Also, using signals and slots to pass information is not the best idea. If it isn't an event, stick to using Q_INVOKABLE

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mshefiti
        wrote on last edited by
        #3

        [quote author="sohail" date="1418664714"]I'm not sure I understand your question. I'd just use a QVariantList in your slot.

        Also, using signals and slots to pass information is not the best idea. If it isn't an event, stick to using Q_INVOKABLE[/quote]

        1. How do I use Q_Invokable from qml? I am using Q_invokable to invoke JS methods from C++, but not the other way around.

        2. If I use QVariantList, need to use QVariantList on the Signal from QML. Here QML:
          @signal qmlSendWorkflowIdsArraySignal(variant workflowIdsArray)@

        So basically I am sending workflowIdsArray, which I defined as type variant.
        What is the syntax in QML to define a signal such that my workflowIdsArray is of type QVariantList?

        I have tried:
        @signal qmlSendWorkflowIdsArraySignal( QVariantList workflowIdsArray);@

        But that is not a valid syntax.

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Hi,

          bq. How to send an array of integers from QML to C++?

          Something like this:
          @
          //cpp
          Q_INVOKABLE void setList(QVariantList arr) {
          qDebug() << arr;
          }

          //qml
          property var arr: [1,5,7]

          Component.onCompleted: {
          obj.setList(arr) //obj=contextproperty object
          }
          @

          157

          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