PDA

View Full Version : Print a QDialog in a PDF file



jjay
22nd February 2008, 15:56
Hi,

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

But when I do that :


QPrinter prn;
prn.setOutputFileName("out.pdf");
QPrintDialog(&prn, mainWindow).exec();
mainWindow.render(&prn);


Only mainWindow is printed :(
How can I print all children ?

Thanks

jpn
23rd February 2008, 09:15
QWidget::render() takes a flag whether to render children or not. By default it does, and it does render children for me:


// main.cpp
#include <QtGui>

class Window : public QMainWindow
{
Q_OBJECT

public:
Window()
{
menuBar()->addAction("Render", this, SLOT(doRender()));

QTextEdit* textEdit = new QTextEdit(this);
textEdit->setHtml("<h1>Hello</h1>");
setCentralWidget(textEdit);
}

private slots:
void doRender()
{
QPixmap pixmap(size());
render(&pixmap);
pixmap.save("render.png");
}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window window;
window.show();
return app.exec();
}

#include "main.moc"

Gopala Krishna
23rd February 2008, 10:47
Hi,

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

But when I do that :


QPrinter prn;
prn.setOutputFileName("out.pdf");
QPrintDialog(&prn, mainWindow).exec();
mainWindow.render(&prn);


Only mainWindow is printed :(
How can I print all children ?

Thanks

Probably the dialog isn't child of mainwindow ?

jjay
23rd February 2008, 17:52
Finaly I tried this but only the last child (QTextEdit) is in my pdf. I expected 2 QpushButton and 1 QTextEdit :(


#include <QApplication>
#include <QDialog>
#include <QPushButton>
#include <QTextEdit>
#include <QPrinter>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QDialog *lc_dialog = new QDialog;
lc_dialog->setGeometry(400,400,500,100);

QPushButton *button1 = new QPushButton(lc_dialog);
QPushButton *button2 = new QPushButton(lc_dialog);
button1->setText("ok");
button2->setText("Concel");

button1->setGeometry(15, lc_dialog->size().height() - 30, lc_dialog->size().width()/2 -20, 30);
button2->setGeometry(lc_dialog->size().width()/2 + 5, lc_dialog->size().height() - 30, lc_dialog->size().width()/2 -20, 30);

QTextEdit *textEdit = new QTextEdit(lc_dialog);
textEdit->setText("Petit texte brut de test");
textEdit->setGeometry(0, 0, lc_dialog->size().width(), 50);

button1->show();
button2->show();
lc_dialog->show();

QPrinter prn;
prn.setOutputFileName("/Path/to/my/pdf/out.pdf");

lc_dialog->render(&prn, QPoint(), QRegion(), QWidget::DrawChildren | QWidget::DrawWindowBackground);

return a.exec();
}


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

jpn
23rd February 2008, 18:01
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.

jjay
23rd February 2008, 23:04
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


#include <QApplication>
#include "MainWindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow *lc_dialog = new MainWindow;
lc_dialog->show();
return a.exec();
}


MainWindow.h :


class MainWindow: public QMainWindow
{
Q_OBJECT
public:
MainWindow();
public slots:
void print();
};


and MainWindow.cpp :


#include "MainWindow.h"
#include <iostream>
#include <QPushButton>
#include <QTextEdit>
#include <QPrinter>

using namespace std;

MainWindow::MainWindow()
:QMainWindow()
{
cout << "MainWindow - start" << endl;
this->setGeometry(400,400,500,100);
QPushButton *button1 = new QPushButton(this);
QPushButton *button2 = new QPushButton(this);
button1->setText("ok");
button2->setText("Concel");
button1->setGeometry(15, this->size().height() - 30, this->size().width()/2 -20, 30);
button2->setGeometry(this->size().width()/2 + 5, this->size().height() - 30, this->size().width()/2 -20, 30);

QTextEdit *textEdit = new QTextEdit(this);
textEdit->setText("Petit texte brut de test");
textEdit->setGeometry(0, 0, this->size().width(), 50);

button1->show();
button2->show();

connect(button1, SIGNAL(clicked()), this, SLOT(print()) );
connect(button2, SIGNAL(clicked()), this, SLOT(print()) );

this->show();
cout << "MainWindow - end" << endl;
}


void MainWindow::print()
{
cout << "print - start" << endl;
QPrinter prn;
prn.setOutputFileName("/Path/to/my/pdf/out.pdf");
render(&prn, QPoint(), QRegion(), QWidget::DrawChildren | QWidget::DrawWindowBackground);
cout << "print - end" << endl;
}


I have the same issue :(
This is the log :

[Session started at 2008-02-23 23:55:39 +0100.]
MainWindow - start
MainWindow - end
print - start
print - end

jjay
28th February 2008, 11:29
There is a bug which has been reported before:
http://trolltech.com/developer/task-tracker/index_html?method=entry&id=192565