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.
Code:
QString Spreadsheet
::toHtml(const QString &plainText
) // converts txt to html for export or printing in this format {
QString str
= Qt
::escape(plainText
);
for (int i = 0; i <RowCount; ++i)
{
if (i > 0)
str += "\n";
for (int j = 0; j <ColumnCount; ++j)
{
if (j > 0)
str += "\t";
str += formula(i, j);
}
}
str.replace("\t", "<td>");
str.replace("\n", "\n<tr><td>");
str.prepend("<table>\n<tr><td>");
str.append("\n</table>");
return str;
// 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
}
void Spreadsheet::PrintAsHtml() // called from signal slot
{
toHtml();
printHtml();
}
void Spreadsheet
::printHtml(const QString &html
) {
#ifndef QT_NO_PRINTER
if (printDialog.exec()) // if the user presses ok then it prints- otherwise exits w/o printing
{
textDocument.setHtml(html);
textDocument.print(&printer);
}
#endif
}
declarations
Code:
public:
void PrintAsHtml();
private:
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?
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
Code:
{
Q_OBJECT
public:// public
// declarations already given in post
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.
Code:
void Spreadsheet::PrintAsHtml() // called from signal slot
{
toHtml(); // return value gets lost
printHtml(); // doesn't receive any parameters/arguments, so it prints an empty string
}
After you store the return value, you can pass it as a parameter/argument to printHtml(), like this:
Code:
void Spreadsheet::PrintAsHtml()
{
printHtml(processedText);
}
Alternatively, you can shorten your code by passing a function (with the appropriate return type) as a parameter/argument:
Code:
void Spreadsheet::PrintAsHtml()
{
printHtml(toHtml());
}
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
Code:
{
Q_OBJECT
// ...
private:
// ...
}
ii) The local variable in your function
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?
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