Question

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 excecute.

C++ 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 Iäm 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?

Was it helpful?

Solution

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 brackets, or else only the first line would execute

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 qt project forum I see this is an issue in Qt4.8 but it has been fixed in Qt5, but since Im using Qt4.8 the brackets will do http://qt-project.org/forums/viewthread/9897

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top