Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Passing QVariant from C++ function return to QML, Qt4.8
Forum Updated to NodeBB v4.3 + New Features

Passing QVariant from C++ function return to QML, Qt4.8

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 3.8k 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.
  • R Offline
    R Offline
    rms25
    wrote on last edited by
    #1

    I have a function in C++ which is called from my QML code. When the function is void or returns an Int everything is fine. However when returning a QVariant and I call the function the function does not exceute

    Cpp function

    @QVariant Wifi::scan(){
    QVariantList varlist;
    QVariant var;

    qDebug("Inside function");
    varlist.append("Test1");
    varlist.append("Test2");
    var = QVariant(varlist);

    return var;
    }@

    .h file

    @include <QVariant>

    class Wifi : public QObject
    {
    Q_OBJECT
    public:
    Wifi();
    Q_INVOKABLE QVariant scan();
    Q_INVOKABLE int checkIfConnected();

    QStringList* getDevice();
    

    };@

    In the QML file "Test" prints but "Test2" does not. "Inside function" from the c++ part doesn't print either. so Im assuming it gets stuck or something in wifi.scan() part

    @console.debug("Test")
    var anArray = wifi.scan()
    for (var i=0; i<anArray.length; i++)
    console.log("Array item:", anArray[i])
    console.debug("Test2")@

    I get no error message of any sort. I have also tried to assign the return value to a "property variant" and just run it on its own (wifi.scan()) without assigning the return value to anything. Any ideas on what could be the problem?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rms25
      wrote on last edited by
      #2

      I saw what the problem was. I had the function call in a switch statement in qml.

      @ switch(returnint){
      case 1:
      loadingtext.text = "Already Connected"
      break;
      case 0: //do scan of networks in range
      console.debug("Test")
      var anArray = wifi.scan()
      for (var i=0; i<anArray.length; i++)
      console.log("Array item:", anArray[i])
      console.debug("Test2")
      break;
      case -1:
      loadingtext.text = "No wireless card found"
      break;
      }
      @

      I just had to put the case statements in brakets, or else only the first line would excecute
      @case 0: //do scan of networks in range
      {
      console.debug("Test")
      var anArray = wifi.scan()
      for (var i=0; i<anArray.length; i++)
      console.log("Array item:", anArray[i])
      console.debug("Test2")
      }
      break;@

      From another thread on the forum I see this issue has been fixed in Qt5, but since Im using Qt4.8 the brackets will do
      http://qt-project.org/forums/viewthread/9897

      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