Passing QVariant from C++ function return to QML, Qt4.8
-
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?
-
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