Results 1 to 7 of 7

Thread: Print a QDialog in a PDF file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Unhappy Print a QDialog in a PDF file

    Hi,

    I have to save a QDialog : mainWindow (with all his children : QTextEdit, QTableView, ...) in a pdf file.

    But when I do that :
    Qt Code:
    1. prn.setOutputFileName("out.pdf");
    2. QPrintDialog(&prn, mainWindow).exec();
    3. mainWindow.render(&prn);
    To copy to clipboard, switch view to plain text mode 

    Only mainWindow is printed
    How can I print all children ?

    Thanks
    Last edited by jpn; 22nd February 2008 at 16:32. Reason: changed [qtclass] to [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Print a QDialog in a PDF file

    QWidget::render() takes a flag whether to render children or not. By default it does, and it does render children for me:
    Qt Code:
    1. // main.cpp
    2. #include <QtGui>
    3.  
    4. class Window : public QMainWindow
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. Window()
    10. {
    11. menuBar()->addAction("Render", this, SLOT(doRender()));
    12.  
    13. QTextEdit* textEdit = new QTextEdit(this);
    14. textEdit->setHtml("<h1>Hello</h1>");
    15. setCentralWidget(textEdit);
    16. }
    17.  
    18. private slots:
    19. void doRender()
    20. {
    21. QPixmap pixmap(size());
    22. render(&pixmap);
    23. pixmap.save("render.png");
    24. }
    25. };
    26.  
    27. int main(int argc, char *argv[])
    28. {
    29. QApplication app(argc, argv);
    30. Window window;
    31. window.show();
    32. return app.exec();
    33. }
    34.  
    35. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    J-P Nurmi

  3. #3
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Print a QDialog in a PDF file

    Quote Originally Posted by jjay View Post
    Hi,

    I have to save a QDialog : mainWindow (with all his children : QTextEdit, QTableView, ...) in a pdf file.

    But when I do that :
    Qt Code:
    1. prn.setOutputFileName("out.pdf");
    2. QPrintDialog(&prn, mainWindow).exec();
    3. mainWindow.render(&prn);
    To copy to clipboard, switch view to plain text mode 

    Only mainWindow is printed
    How can I print all children ?

    Thanks
    Probably the dialog isn't child of mainwindow ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  4. #4
    Join Date
    Feb 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Print a QDialog in a PDF file

    Finaly I tried this but only the last child (QTextEdit) is in my pdf. I expected 2 QpushButton and 1 QTextEdit

    Qt Code:
    1. #include <QApplication>
    2. #include <QDialog>
    3. #include <QPushButton>
    4. #include <QTextEdit>
    5. #include <QPrinter>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10.  
    11. QDialog *lc_dialog = new QDialog;
    12. lc_dialog->setGeometry(400,400,500,100);
    13.  
    14. QPushButton *button1 = new QPushButton(lc_dialog);
    15. QPushButton *button2 = new QPushButton(lc_dialog);
    16. button1->setText("ok");
    17. button2->setText("Concel");
    18.  
    19. button1->setGeometry(15, lc_dialog->size().height() - 30, lc_dialog->size().width()/2 -20, 30);
    20. button2->setGeometry(lc_dialog->size().width()/2 + 5, lc_dialog->size().height() - 30, lc_dialog->size().width()/2 -20, 30);
    21.  
    22. QTextEdit *textEdit = new QTextEdit(lc_dialog);
    23. textEdit->setText("Petit texte brut de test");
    24. textEdit->setGeometry(0, 0, lc_dialog->size().width(), 50);
    25.  
    26. button1->show();
    27. button2->show();
    28. lc_dialog->show();
    29.  
    30. QPrinter prn;
    31. prn.setOutputFileName("/Path/to/my/pdf/out.pdf");
    32.  
    33. lc_dialog->render(&prn, QPoint(), QRegion(), QWidget::DrawChildren | QWidget::DrawWindowBackground);
    34.  
    35. return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

    In attachement :
    - out.pdf : The pdf generated
    - image 2.png the picture expected
    Attached Images Attached Images
    Attached Files Attached Files

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Print a QDialog in a PDF file

    I suppose the problem is that the dialog, including its children, isn't even visible yet by the time you attempt to render it. QWidget::show() doesn't show the dialog immediately but schedules a show event. Widgets aren't usually polished until necessary eg. when they become visible.
    J-P Nurmi

  6. #6
    Join Date
    Feb 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Print a QDialog in a PDF file

    Quote Originally Posted by jpn View Post
    I suppose the problem is that the dialog, including its children, isn't even visible yet by the time you attempt to render it. QWidget::show() doesn't show the dialog immediately but schedules a show event. Widgets aren't usually polished until necessary eg. when they become visible.
    Ok, so i tried this :

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "MainWindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow *lc_dialog = new MainWindow;
    8. lc_dialog->show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow.h :
    Qt Code:
    1. class MainWindow: public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. MainWindow();
    6. public slots:
    7. void print();
    8. };
    To copy to clipboard, switch view to plain text mode 

    and MainWindow.cpp :
    Qt Code:
    1. #include "MainWindow.h"
    2. #include <iostream>
    3. #include <QPushButton>
    4. #include <QTextEdit>
    5. #include <QPrinter>
    6.  
    7. using namespace std;
    8.  
    9. MainWindow::MainWindow()
    10. {
    11. cout << "MainWindow - start" << endl;
    12. this->setGeometry(400,400,500,100);
    13. QPushButton *button1 = new QPushButton(this);
    14. QPushButton *button2 = new QPushButton(this);
    15. button1->setText("ok");
    16. button2->setText("Concel");
    17. button1->setGeometry(15, this->size().height() - 30, this->size().width()/2 -20, 30);
    18. button2->setGeometry(this->size().width()/2 + 5, this->size().height() - 30, this->size().width()/2 -20, 30);
    19.  
    20. QTextEdit *textEdit = new QTextEdit(this);
    21. textEdit->setText("Petit texte brut de test");
    22. textEdit->setGeometry(0, 0, this->size().width(), 50);
    23.  
    24. button1->show();
    25. button2->show();
    26.  
    27. connect(button1, SIGNAL(clicked()), this, SLOT(print()) );
    28. connect(button2, SIGNAL(clicked()), this, SLOT(print()) );
    29.  
    30. this->show();
    31. cout << "MainWindow - end" << endl;
    32. }
    33.  
    34.  
    35. void MainWindow::print()
    36. {
    37. cout << "print - start" << endl;
    38. QPrinter prn;
    39. prn.setOutputFileName("/Path/to/my/pdf/out.pdf");
    40. render(&prn, QPoint(), QRegion(), QWidget::DrawChildren | QWidget::DrawWindowBackground);
    41. cout << "print - end" << endl;
    42. }
    To copy to clipboard, switch view to plain text mode 

    I have the same issue
    This is the log :
    Qt Code:
    1. [Session started at 2008-02-23 23:55:39 +0100.]
    2. MainWindow - start
    3. MainWindow - end
    4. print - start
    5. print - end
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Unhappy Re: Print a QDialog in a PDF file

    There is a bug which has been reported before:
    http://trolltech.com/developer/task-...ntry&id=192565

Similar Threads

  1. how to print an html file?
    By patcito in forum Qt Programming
    Replies: 5
    Last Post: 31st August 2008, 16:50
  2. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  3. file renaming on windows
    By jdd81 in forum Qt Programming
    Replies: 9
    Last Post: 2nd October 2007, 19:41
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10

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.