Hi,
I have to save a QDialog : mainWindow (with all his children : QTextEdit, QTableView, ...) in a pdf file.
But when I do that :
Only mainWindow is printed :(
How can I print all children ?
Thanks
Printable View
Hi,
I have to save a QDialog : mainWindow (with all his children : QTextEdit, QTableView, ...) in a pdf file.
But when I do that :
Only mainWindow is printed :(
How can I print all children ?
Thanks
QWidget::render() takes a flag whether to render children or not. By default it does, and it does render children for me:
Code:
// main.cpp #include <QtGui> { Q_OBJECT public: Window() { menuBar()->addAction("Render", this, SLOT(doRender())); textEdit->setHtml("<h1>Hello</h1>"); setCentralWidget(textEdit); } private slots: void doRender() { render(&pixmap); pixmap.save("render.png"); } }; int main(int argc, char *argv[]) { Window window; window.show(); return app.exec(); } #include "main.moc"
Finaly I tried this but only the last child (QTextEdit) is in my pdf. I expected 2 QpushButton and 1 QTextEdit :(
Code:
#include <QApplication> #include <QDialog> #include <QPushButton> #include <QTextEdit> #include <QPrinter> int main(int argc, char *argv[]) { lc_dialog->setGeometry(400,400,500,100); 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); 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"); return a.exec(); }
In attachement :
- out.pdf : The pdf generated
- image 2.png the picture expected
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
Code:
#include <QApplication> #include "MainWindow.h" int main(int argc, char *argv[]) { MainWindow *lc_dialog = new MainWindow; lc_dialog->show(); return a.exec(); }
MainWindow.h :
Code:
{ Q_OBJECT public: MainWindow(); public slots: void print(); };
and MainWindow.cpp :
Code:
#include "MainWindow.h" #include <iostream> #include <QPushButton> #include <QTextEdit> #include <QPrinter> using namespace std; MainWindow::MainWindow() { cout << "MainWindow - start" << endl; this->setGeometry(400,400,500,100); 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); 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"); cout << "print - end" << endl; }
I have the same issue :(
This is the log :
Code:
[Session started at 2008-02-23 23:55:39 +0100.] MainWindow - start MainWindow - end print - start print - end
There is a bug which has been reported before:
http://trolltech.com/developer/task-...ntry&id=192565