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());
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());