{
Q_OBJECT
public:
Report(const QString& theTitle, int theNumberOfPages) : itsTitle(theTitle), itsNumberOfPages(theNumberOfPages) {}
void preview();
void printPage
(QPainter* thePainter,
int thePage
);
private slots:
void printDocument
(QPrinter* thePrinter
);
private:
int itsNumberOfPages;
};
void Report::preview()
{
QPrintPreviewDialog aPreview(&aPrinter, 0);
connect(&aPreview,
SIGNAL(paintRequested
(QPrinter *)),
this,
SLOT(printDocument
(QPrinter *)));
aPreview.exec();
}
void Report
::printDocument(QPrinter* thePrinter
) {
thePrinter->setFromTo(1, itsNumberOfPages);
aProgress.setWindowModality(Qt::ApplicationModal);
aProgress.setWindowTitle(itsTitle);
aProgress.setMinimum(thePrinter->fromPage() - 1);
aProgress.setMaximum(thePrinter->toPage());
// Configure a painter with local coordinate system in 1/10th of a millimeter based on an A4 landscape page
aPainter.begin(thePrinter);
int aLogicalWidth = 21000;
int aLogicalHeight = 29700;
aPainter.setWindow(0, 0, aLogicalWidth, aLogicalHeight);
int aPhysicalWidth = thePrinter->width();
int aPhysicalHeight = thePrinter->height();
aPainter.setViewport(0, 0, aPhysicalWidth, aPhysicalHeight);
// Print all pages
bool aFirstPage = true;
for (int aPage = thePrinter->fromPage(); aPage <= thePrinter->toPage(); ++aPage)
{
if (!aFirstPage)
thePrinter->newPage();
qApp->processEvents();
if (aProgress.wasCanceled())
break;
// Print page
printPage(&aPainter, aPage);
aProgress.setValue(aPage);
aFirstPage = false;
}
aPainter.end();
}
void Report
::printPage(QPainter* thePainter,
int thePage
) {
// Set pen
aPen.setColor(Qt::black);
aPen.setWidth(0);
aPen.setStyle(Qt::SolidLine);
thePainter->setPen(aPen);
// Set brush
aBrush.setColor(Qt::black);
aBrush.setStyle(Qt::SolidPattern);
thePainter->setBrush(aBrush);
// Set font
aFont.setFamily("Arial");
aFont.setPixelSize(100); // ??? how to set the font size to 1cm ???
aFont.
setWeight(QFont::Normal);
aFont.setItalic(false);
thePainter->setFont(aFont);
// Draw line
thePainter
->drawLine
(QPoint(1000,
1000),
QPoint(5000,
5000));
// Print a rectangle
thePainter
->drawRect
(QRect(8000,
8000,
2000,
2000));
// Print a text
thePainter->drawText(1000, 12000, aText);
}
class Report : public QObject
{
Q_OBJECT
public:
Report(const QString& theTitle, int theNumberOfPages) : itsTitle(theTitle), itsNumberOfPages(theNumberOfPages) {}
void preview();
void printPage(QPainter* thePainter, int thePage);
private slots:
void printDocument(QPrinter* thePrinter);
private:
QString itsTitle;
int itsNumberOfPages;
};
void Report::preview()
{
QPrinter aPrinter(QPrinter::HighResolution);
QPrintPreviewDialog aPreview(&aPrinter, 0);
connect(&aPreview, SIGNAL(paintRequested(QPrinter *)), this, SLOT(printDocument(QPrinter *)));
aPreview.exec();
}
void Report::printDocument(QPrinter* thePrinter)
{
thePrinter->setFromTo(1, itsNumberOfPages);
QString aTitle = QString(tr("Preparing %1")).arg(itsTitle);
QProgressDialog aProgress(aTitle, tr("&Cancel"), 0, itsNumberOfPages, 0);
aProgress.setWindowModality(Qt::ApplicationModal);
aProgress.setWindowTitle(itsTitle);
aProgress.setMinimum(thePrinter->fromPage() - 1);
aProgress.setMaximum(thePrinter->toPage());
// Configure a painter with local coordinate system in 1/10th of a millimeter based on an A4 landscape page
QPainter aPainter;
aPainter.begin(thePrinter);
int aLogicalWidth = 21000;
int aLogicalHeight = 29700;
aPainter.setWindow(0, 0, aLogicalWidth, aLogicalHeight);
int aPhysicalWidth = thePrinter->width();
int aPhysicalHeight = thePrinter->height();
aPainter.setViewport(0, 0, aPhysicalWidth, aPhysicalHeight);
// Print all pages
bool aFirstPage = true;
for (int aPage = thePrinter->fromPage(); aPage <= thePrinter->toPage(); ++aPage)
{
if (!aFirstPage)
thePrinter->newPage();
qApp->processEvents();
if (aProgress.wasCanceled())
break;
// Print page
printPage(&aPainter, aPage);
aProgress.setValue(aPage);
aFirstPage = false;
}
aPainter.end();
}
void Report::printPage(QPainter* thePainter, int thePage)
{
// Set pen
QPen aPen;
aPen.setColor(Qt::black);
aPen.setWidth(0);
aPen.setStyle(Qt::SolidLine);
thePainter->setPen(aPen);
// Set brush
QBrush aBrush;
aBrush.setColor(Qt::black);
aBrush.setStyle(Qt::SolidPattern);
thePainter->setBrush(aBrush);
// Set font
QFont aFont;
aFont.setFamily("Arial");
aFont.setPixelSize(100); // ??? how to set the font size to 1cm ???
aFont.setWeight(QFont::Normal);
aFont.setItalic(false);
thePainter->setFont(aFont);
// Draw line
thePainter->drawLine(QPoint(1000, 1000), QPoint(5000, 5000));
// Print a rectangle
thePainter->drawRect(QRect(8000, 8000, 2000, 2000));
// Print a text
QString aText = QString("%1 on page %2").arg(itsTitle).arg(thePage);
thePainter->drawText(1000, 12000, aText);
}
To copy to clipboard, switch view to plain text mode
Bookmarks