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