I have some issues with simple reports that my application makes by QTextDocument, html-page (template), QPrinter and QPrintPreviewDialog. I made classes for this stuff.
But recently I was realised that I have memory leak. so I tried to find where is it.
It seems that I found out smth but don't know exactly why this happening..
I threw out spec. classes and wrote just necessary code:
........
// in widget's constructor
........
// in widget'd destructor
delete document;
........
//----------------------------------------------------------------------------------------
void fmPartStatInRange::previewSelectionReport() {
QMessageBox::critical (this,
"", tr
("Error opening report file %1").
arg (file.
fileName ()));
return;
}
stream.
setCodec (QTextCodec::codecForName ("UTF-8"));
document->clear();
document->setHtml (stream.readAll ());
file.close ();
//----------------------------------------------------------------------------------------
void fmPartStatInRange
::print(QPrinter *printer
) { #ifdef QT_NO_PRINTER
Q_UNUSED(printer);
#else
document->print(printer);
#endif
}
//----------------------------------------------------------------------------------------
void fmPartStatInRange::printReport() {
#ifndef QT_NO_PRINTER
QPrintPreviewDialog preview(&printer, this);
preview.exec();
#endif
}
//----------------------------------------------------------------------------------------
void fmPartStatInRange::createReport() {
previewSelectionReport();
if (document)
printReport();
}
//----------------------------------------------------------------------------------------
QTextDocument* document;
........
// in widget's constructor
document = new QTextDocument();
........
// in widget'd destructor
delete document;
........
//----------------------------------------------------------------------------------------
void fmPartStatInRange::previewSelectionReport() {
QFile file (QCoreApplication::applicationDirPath () + "/reports/statisticPartsInRange.html");
if (!file.open (QIODevice::ReadOnly)) {
QMessageBox::critical (this, "", tr ("Error opening report file %1").arg (file.fileName ()));
return;
}
QTextStream stream (&file);
stream.setCodec (QTextCodec::codecForName ("UTF-8"));
document->clear();
document->setHtml (stream.readAll ());
file.close ();
//----------------------------------------------------------------------------------------
void fmPartStatInRange::print(QPrinter *printer) {
#ifdef QT_NO_PRINTER
Q_UNUSED(printer);
#else
document->print(printer);
#endif
}
//----------------------------------------------------------------------------------------
void fmPartStatInRange::printReport() {
#ifndef QT_NO_PRINTER
QPrinter printer(QPrinter::HighResolution);
QPrintPreviewDialog preview(&printer, this);
connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(print(QPrinter*)));
preview.exec();
#endif
}
//----------------------------------------------------------------------------------------
void fmPartStatInRange::createReport() {
previewSelectionReport();
if (document)
printReport();
}
//----------------------------------------------------------------------------------------
To copy to clipboard, switch view to plain text mode
I execute createReport() many times and see memory leak.
But if I comment this line:
document->setHtml (stream.readAll ());
document->setHtml (stream.readAll ());
To copy to clipboard, switch view to plain text mode
that's all ok. no memory leak.
of course in this case I don't get any sence of report only white page.
I use 4.7.3 Qt ver.
unfortunately I haven't any opportunities to check out it on new Qt ver.
so what is wrong with this code or maybe it's a bug in Qt lib?
thanks in advance!
Bookmarks