PDA

View Full Version : passing return value from one function as the parameter of another



hollowhead
1st April 2010, 13:06
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.



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
QPrinter printer(QPrinter::ScreenResolution); // get a scoping error w/o this
QPrintDialog printDialog(&printer, this); // call printer
if (printDialog.exec()) // if the user presses ok then it prints- otherwise exits w/o printing
{
QTextDocument textDocument; // #include <QTextDocument> required
textDocument.setHtml(html);
textDocument.print(&printer);
}
#endif
}


declarations




public:

void PrintAsHtml();

private:
QString plainText;
void printHtml(const QString &html= QString());
QString toHtml(const QString &plainText= QString());

Lykurg
1st April 2010, 13:18
It because you don't pass "html" or "plainText" to your functions! What is Spreadsheet's base class?

hollowhead
1st April 2010, 15:04
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




class Spreadsheet : public QTableWidget

{
Q_OBJECT

public:// public


// declarations already given in post

hackerNovitiate
2nd April 2010, 05:46
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.

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:

void Spreadsheet::PrintAsHtml()
{
QString processedText = toHtml();
printHtml(processedText);
}

Alternatively, you can shorten your code by passing a function (with the appropriate return type) as a parameter/argument:

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
class Spreadsheet : public QTableWidget
{
Q_OBJECT

// ...

private:
QString plainText;

// ...
}

ii) The local variable in your function
QString Spreadsheet::toHtml(const QString &plainText);

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?

hollowhead
2nd April 2010, 11:04
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