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
}
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
}
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:
void Spreadsheet::PrintAsHtml()
{
printHtml(processedText);
}
void Spreadsheet::PrintAsHtml()
{
QString processedText = toHtml();
printHtml(processedText);
}
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:
void Spreadsheet::PrintAsHtml()
{
printHtml(toHtml());
}
void Spreadsheet::PrintAsHtml()
{
printHtml(toHtml());
}
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
{
Q_OBJECT
// ...
private:
// ...
}
class Spreadsheet : public QTableWidget
{
Q_OBJECT
// ...
private:
QString plainText;
// ...
}
To copy to clipboard, switch view to plain text mode
ii) The local variable in your function
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?
Bookmarks