Results 1 to 4 of 4

Thread: How to return QString from function

  1. #1
    Join Date
    Nov 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default How to return QString from function

    Hi, I want to return QString from this function:
    Qt Code:
    1. QString MainWindow::postLogin(QNetworkReply *reply)
    2. {
    3. QString token;
    4.  
    5. (...)
    6.  
    7. return token;
    8. }
    To copy to clipboard, switch view to plain text mode 

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

    Thanks
    Last edited by Rondle; 9th November 2012 at 18:24.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to return QString from function

    learn to c++

    Qt Code:
    1. class myclass
    2. {
    3. public:
    4. std::string func()
    5. {
    6. return "abc";
    7. }
    8.  
    9. void use_func()
    10. {
    11. std::string s = func();
    12.  
    13. // do something with s...
    14. }
    15. };
    16.  
    17. int main()
    18. {
    19. myclass mc;
    20. mc.use_func();
    21. }
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to return QString from function

    You can call this function by:

    QString returnValue = postLogin(reply);

    make this returnValue parameter declaration as global one.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to return QString from function

    Quote Originally Posted by Rondle View Post
    Hi, I want to return QString from this function:
    Qt Code:
    1. QString MainWindow::postLogin(QNetworkReply *reply)
    2. {
    3. QString token;
    4.  
    5. (...)
    6.  
    7. return token;
    8. }
    To copy to clipboard, switch view to plain text mode 

    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,
    _

Similar Threads

  1. Carriage Return in QString
    By incapacitant in forum Newbie
    Replies: 7
    Last Post: 2nd December 2010, 09:18
  2. Replies: 4
    Last Post: 2nd April 2010, 10:04
  3. Replies: 4
    Last Post: 1st February 2010, 14:21
  4. Function return
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2009, 00:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.