Hi,
My goal is to create a print preview function which allows users to see the crossword they've created as it would appear on a piece of paper. Reading the documentation I thought this would be a piece of cake. Here's what I've got so far (taken from the documentation describing how to print a widget):
void MyWindow::filePrintPreview()
{
painter.begin(&printer);
double xscale = printer.pageRect().width()/double(cwArea->width());
double yscale = printer.pageRect().height()/double(cwArea->height());
double scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
printer.paperRect().y() + printer.pageRect().height()/2);
painter.scale(scale, scale);
painter.translate(-width()/2, -height()/2);
cwArea->render(&painter);
QPrintPreviewDialog preview(&printer, this);
preview.exec();
}
void MyWindow::filePrintPreview()
{
QPrinter printer;
QPainter painter;
painter.begin(&printer);
double xscale = printer.pageRect().width()/double(cwArea->width());
double yscale = printer.pageRect().height()/double(cwArea->height());
double scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
printer.paperRect().y() + printer.pageRect().height()/2);
painter.scale(scale, scale);
painter.translate(-width()/2, -height()/2);
cwArea->render(&painter);
QPrintPreviewDialog preview(&printer, this);
connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(print(QPrinter*)));
preview.exec();
}
To copy to clipboard, switch view to plain text mode
MyWindow is my QMainWindow and filePrintPreview() is the slot called when "file->print preview" is triggered.
cwArea is the widget containing the crossword squares.
I read that if I want to render the contents of a widget, these contents have to be children of the widget itself. Since each crossword square is an overridden QTextEdit I added an optional parent parameter to each of the constructors...
{
(...)
}
Tile::Tile(..., QWidget *parent)
{
(...)
}
To copy to clipboard, switch view to plain text mode
...and let the crossword widget be each square's parent. For example:
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cwlayout->addWidget(new Tile(...,this),i*4,j*4,4,4);
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cwlayout->addWidget(new Tile(...,this),i*4,j*4,4,4);
}
}
To copy to clipboard, switch view to plain text mode
Unfortunately all I've managed to preview so far is absolutely nothing.
What have I done wrong? Is it a problem when the child widgets of the widget I wish to print are QTextEdits?
Thanks in advance!
Bookmarks