Results 1 to 6 of 6

Thread: Print preview of a QWidget

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2013
    Location
    Kungsbacka, Sweden
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Print preview of a QWidget

    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):

    Qt Code:
    1. void MyWindow::filePrintPreview()
    2. {
    3. QPrinter printer;
    4. QPainter painter;
    5. painter.begin(&printer);
    6. double xscale = printer.pageRect().width()/double(cwArea->width());
    7. double yscale = printer.pageRect().height()/double(cwArea->height());
    8. double scale = qMin(xscale, yscale);
    9. painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
    10. printer.paperRect().y() + printer.pageRect().height()/2);
    11. painter.scale(scale, scale);
    12. painter.translate(-width()/2, -height()/2);
    13. cwArea->render(&painter);
    14. QPrintPreviewDialog preview(&printer, this);
    15. connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(print(QPrinter*)));
    16. preview.exec();
    17. }
    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...

    Qt Code:
    1. Tile::Tile(..., QWidget *parent)
    2. {
    3. (...)
    4. }
    To copy to clipboard, switch view to plain text mode 

    ...and let the crossword widget be each square's parent. For example:

    Qt Code:
    1. for(int i = 0; i < rows; i++)
    2. {
    3. for(int j = 0; j < columns; j++)
    4. {
    5. cwlayout->addWidget(new Tile(...,this),i*4,j*4,4,4);
    6. }
    7. }
    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!

  2. The following user says thank you to Ponnytail for this useful post:


Similar Threads

  1. pyqt print preview QTableView
    By iraj_jelo in forum Qt Programming
    Replies: 0
    Last Post: 23rd May 2012, 08:44
  2. Print preview on QGraphicsItem
    By lni in forum Qt Programming
    Replies: 0
    Last Post: 13th April 2012, 05:25
  3. Print preview for Qt activex
    By sowmya in forum Qt Programming
    Replies: 0
    Last Post: 19th July 2010, 10:53
  4. print preview
    By wbt_ph in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2009, 06:41
  5. Cancelling Long-running Print/Print Preview
    By ChrisW67 in forum Newbie
    Replies: 4
    Last Post: 16th June 2009, 23:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.