Results 1 to 2 of 2

Thread: Issue with getting a QString output through a function

  1. #1
    Join Date
    Feb 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Issue with getting a QString output through a function

    Hi

    I have a problem where I can't seem to get a output to display in a console when doing it through a function.

    It works when doing it through Main(), but just blank when doing it through the function.

    Below is some of my code:

    Qt Code:
    1. //Main()
    2.  
    3. #include "ConferencePaper.h"
    4. #include "JournalArticle.h"
    5. #include "Reference.h"
    6. #include <QDebug>
    7. #include <QTextStream>
    8.  
    9. QTextStream cout(stdout);
    10.  
    11. int main()
    12. {
    13. //QApplication app(argc, argv);
    14. QStringList list1;
    15. list1 << "This is a test";
    16.  
    17. Reference a("Marius",list1,1,"c"); //Instance of the Reference class created with parameter values
    18. cout << "Title: " << a.getTitle(); //This works fine
    19. a.toString();
    20.  
    21. return 0;
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    //Reference Function
    Qt Code:
    1. #include <QString>
    2. #include <QStringList>
    3. #include <QTextStream>
    4.  
    5. #include "Reference.h"
    6.  
    7. Reference::Reference(QString ti, QStringList as, int ye, QString id): title(ti), authors(as), year(ye), refID(id){}
    8.  
    9. QString Reference::toString()
    10. {
    11. return QString("Title: %1\n") .arg(getTitle()); //Does not display anything
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Last edited by anda_skoa; 12th February 2016 at 11:01. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issue with getting a QString output through a function

    Reference::toString() does not print anything; it just builds and returns a QString. The call
    Qt Code:
    1. a.toString();
    To copy to clipboard, switch view to plain text mode 
    in main() therefore builds a QString and throws it away immediately. What you probably intended was to print the QString returned by the call, as in:
    Qt Code:
    1. cout << a.toString();
    To copy to clipboard, switch view to plain text mode 

    If I were you I would choose another name for the QTextStream; readers of your code will assume cout refers to std::cout.

Similar Threads

  1. Replies: 7
    Last Post: 1st February 2013, 01:36
  2. QString function by value
    By hmarani in forum Newbie
    Replies: 6
    Last Post: 24th November 2010, 11:01
  3. Get the qDebug() output to QString
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 8th March 2010, 02:09
  4. Replies: 3
    Last Post: 18th October 2007, 19:07
  5. Using QString in C function ...
    By Godlike in forum Newbie
    Replies: 10
    Last Post: 8th April 2006, 18:01

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.