PDA

View Full Version : How to return QString from function



Rondle
9th November 2012, 17:10
Hi, I want to return QString from this function:

QString MainWindow::postLogin(QNetworkReply *reply)
{
QString token;

(...)

return token;
}

Is this good way? And how can i call the function and use this QString value in my main program?

Thanks

amleto
9th November 2012, 19:09
learn to c++ :)




class myclass
{
public:
std::string func()
{
return "abc";
}

void use_func()
{
std::string s = func();

// do something with s...
}
};

int main()
{
myclass mc;
mc.use_func();
}

Gokulnathvc
21st November 2012, 07:00
You can call this function by:

QString returnValue = postLogin(reply);

make this returnValue parameter declaration as global one.

anda_skoa
21st November 2012, 18:51
Hi, I want to return QString from this function:

QString MainWindow::postLogin(QNetworkReply *reply)
{
QString token;

(...)

return token;
}

Is this good way?


Yes.



And how can i call the function and use this QString value in my main program?


Gokulnathvc already answered that when you are in the scope of the MainWindow class (which is likely).

You can treat QString as a return value just like you would treat any of the basic types, e.g. int or bool.
QString, like a lot of other Qt classes, uses a technique calles "implicit sharing", meaing that copying a QString value is not incurring the cost (as in CPU and memory usage) of copying the actual string data. So like returning an int it is very cheap.

Cheers,
_