Results 1 to 5 of 5

Thread: passing return value from one function as the parameter of another

  1. #1
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default passing return value from one function as the parameter of another

    I hesitate to ask this question since it seems in a way so basic, but despite looking at my C++ books and online I cannot work out what to do. Originally I called printHtml(str) within the toHtml() function and the code primative that it is (I accept) works. But I want to use the toHtml() code at least two more times in other functions so I setup the print function separately. Now I get a print dialog but nothing prints out accept bizarrely except a "1" in the bottom right hand side of the page! I'm pretty sure I know what the problem is, that I'm not passing the string from the first function toHtml() to the second printHtml. I've tried several different things but none work, what is the best way to pass from one to the other, by reference? If so how? Any help would be really kind.

    Qt Code:
    1. QString Spreadsheet::toHtml(const QString &plainText) // converts txt to html for export or printing in this format
    2. {
    3. QString str = Qt::escape(plainText);
    4.  
    5. for (int i = 0; i <RowCount; ++i)
    6. {
    7. if (i > 0)
    8. str += "\n";
    9. for (int j = 0; j <ColumnCount; ++j)
    10. {
    11. if (j > 0)
    12. str += "\t";
    13. str += formula(i, j);
    14. }
    15. }
    16. str.replace("\t", "<td>");
    17. str.replace("\n", "\n<tr><td>");
    18. str.prepend("<table>\n<tr><td>");
    19. str.append("\n</table>");
    20. return str;
    21.  
    22. // printHtml(str); this works but I want to use the toHtml() function at least twice more and this is inefficient so I set up a separate function
    23. }
    24.  
    25. void Spreadsheet::PrintAsHtml() // called from signal slot
    26. {
    27. toHtml();
    28. printHtml();
    29. }
    30.  
    31. void Spreadsheet::printHtml(const QString &html)
    32. {
    33. #ifndef QT_NO_PRINTER
    34. QPrinter printer(QPrinter::ScreenResolution); // get a scoping error w/o this
    35. QPrintDialog printDialog(&printer, this); // call printer
    36. if (printDialog.exec()) // if the user presses ok then it prints- otherwise exits w/o printing
    37. {
    38. QTextDocument textDocument; // #include <QTextDocument> required
    39. textDocument.setHtml(html);
    40. textDocument.print(&printer);
    41. }
    42. #endif
    43. }
    To copy to clipboard, switch view to plain text mode 

    declarations

    Qt Code:
    1. public:
    2.  
    3. void PrintAsHtml();
    4.  
    5. private:
    6. QString plainText;
    7. void printHtml(const QString &html= QString());
    8. QString toHtml(const QString &plainText= QString());
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: passing return value from one function as the parameter of another

    It because you don't pass "html" or "plainText" to your functions! What is Spreadsheet's base class?

  3. #3
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: passing return value from one function as the parameter of another

    Thanks for your reply as always. I know, the problem is I don't know how to do it.... My book suggest passing by ref but I'm still unsure how. I tried adding plainText as an parameter to printHtml(); but it didn't do any different.

    Spreadsheet

    Qt Code:
    1. class Spreadsheet : public QTableWidget
    2.  
    3. {
    4. Q_OBJECT
    5.  
    6. public:// public
    7.  
    8.  
    9. // declarations already given in post
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2010
    Location
    Perth, Australia
    Posts
    37
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing return value from one function as the parameter of another

    You can store the return value of toHtml(), the same way you store the return value of a C function.

    In your code below, toHtml() processes your text properly. But because the return value isn't assigned to anything, the processed data in 'str' gets discarded.
    Qt Code:
    1. void Spreadsheet::PrintAsHtml() // called from signal slot
    2. {
    3. toHtml(); // return value gets lost
    4. printHtml(); // doesn't receive any parameters/arguments, so it prints an empty string
    5. }
    To copy to clipboard, switch view to plain text mode 

    After you store the return value, you can pass it as a parameter/argument to printHtml(), like this:
    Qt Code:
    1. void Spreadsheet::PrintAsHtml()
    2. {
    3. QString processedText = toHtml();
    4. printHtml(processedText);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Alternatively, you can shorten your code by passing a function (with the appropriate return type) as a parameter/argument:
    Qt Code:
    1. void Spreadsheet::PrintAsHtml()
    2. {
    3. printHtml(toHtml());
    4. }
    To copy to clipboard, switch view to plain text mode 




    Regarding references and passing by references, have a look at Section 8 of http://www.4p8.com/eric.brasseur/cppcen.html#l8 . I read from another post that you know C, so the rest of the tutorial might be useful as well!



    By the way, right now, your implementation of 'toHtml' is dangerous because your code has 2 different variables called 'plainText', which makes it easy for a reader of your code to get confused:

    i) The instance variable for your Spreadsheet class
    Qt Code:
    1. class Spreadsheet : public QTableWidget
    2. {
    3. Q_OBJECT
    4.  
    5. // ...
    6.  
    7. private:
    8. QString plainText;
    9.  
    10. // ...
    11. }
    To copy to clipboard, switch view to plain text mode 

    ii) The local variable in your function
    Qt Code:
    1. QString Spreadsheet::toHtml(const QString &plainText);
    To copy to clipboard, switch view to plain text mode 

    Do you want your function to convert ALL of the plainText in your Spreadsheet object, or do you want to select different bits of text to pass to 'toHtml' each time?

  5. #5
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: passing return value from one function as the parameter of another

    Thankyou very much for your helpful comments the tutorial looks very useful I've bookmarked it. How do you formally thank someone I cannot see how to do it? Neil

Similar Threads

  1. Replies: 4
    Last Post: 1st February 2010, 14:21
  2. Replies: 4
    Last Post: 12th August 2008, 01:55
  3. Replies: 3
    Last Post: 17th July 2008, 07:43
  4. Passing Ui files as parameter
    By hgedek in forum Newbie
    Replies: 3
    Last Post: 12th December 2007, 12:14
  5. Passing QFile objecrt as parameter
    By db in forum Newbie
    Replies: 2
    Last Post: 28th August 2007, 16:09

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.