schall_l
24th July 2009, 21:04
Hi,
My PrintableTableView class deriving from QTableView is using QWebView to print the content of my model:
#ifndef PRINTABLETABLEVIEW_H
#define PRINTABLETABLEVIEW_H
// QT includes
#include <QtWebKit>
#include <QtGui>
// non QT includes
class PrintableTableView : public QTableView {
Q_OBJECT
public:
PrintableTableView ( QWidget * parent = 0 );
~PrintableTableView();
public slots:
virtual void print();
void printHtml(bool ok);
protected:
// Webkit things for printing the identifiers
QString readFile(const QString &name);
QWebView *m_webview;
QString m_htmlTemplate;
QUrl m_baseUrl;
};
#endif
// non QT includes
#include "PrintableTableView.h"
#include "mainWindow.h"
PrintableTableView::PrintableTableView( QWidget * parent ): QTableView(parent){
m_webview = new QWebView(this);
m_webview->setVisible(false);
}
PrintableTableView::~PrintableTableView(){
}
void
PrintableTableView::print() {
QString html;
MainWindow *mainWindow = MainWindow::instance();
// Header
html= QString("\
<div id=header>\
<div id=logo>\
<table border=0>\
<tr>\
<th><h1>Empty page</h1></th>\
<th><h2>from %1</h2></th>\
</tr>\
</table>\
<h3>printed on %2</h3>\
</div>\
</div>").arg(mainWindow->currentProjectName()).arg(QDate::currentDate().toS tring());
m_htmlTemplate = readFile(QLatin1String(":/identifiers.html"));
m_htmlTemplate.replace(QLatin1String("<!-- CONTENT -->"), html);
m_baseUrl = QUrl(QLatin1String("qrc:/identifiers.html"));
connect(m_webview, SIGNAL(loadFinished(bool)),this, SLOT(printHtml(bool)));
m_webview->setHtml(m_htmlTemplate, m_baseUrl);
}
void
PrintableTableView::printHtml(bool ok) {
disconnect(m_webview, SIGNAL(loadFinished(bool)),this, SLOT(printHtml(bool)));
QPrinter printer;
QPrintDialog printDialog(&printer, this);
printDialog.setOption(QAbstractPrintDialog::PrintS election,false);
printDialog.setOption(QAbstractPrintDialog::PrintP ageRange,false);
if (printDialog.exec() == QDialog::Accepted) {
m_webview->print(&printer);
}
}
QString
PrintableTableView::readFile(const QString &name)
{
QFile f(name);
if (!f.open(QIODevice::ReadOnly)) {
qWarning("Unable to open %s: %s", name.toUtf8().constData(), f.errorString().toUtf8().constData());
return QString();
}
QTextStream ts(&f);
return ts.readAll();
}
The content of the QTableView is displayed correctly before and while I have the printDialog open. Sometimes (yes sometimes, not everytime) it happens that the content of the QTableView disapear if I cancel the printDialog.
What I am doing wrong ?
My PrintableTableView class deriving from QTableView is using QWebView to print the content of my model:
#ifndef PRINTABLETABLEVIEW_H
#define PRINTABLETABLEVIEW_H
// QT includes
#include <QtWebKit>
#include <QtGui>
// non QT includes
class PrintableTableView : public QTableView {
Q_OBJECT
public:
PrintableTableView ( QWidget * parent = 0 );
~PrintableTableView();
public slots:
virtual void print();
void printHtml(bool ok);
protected:
// Webkit things for printing the identifiers
QString readFile(const QString &name);
QWebView *m_webview;
QString m_htmlTemplate;
QUrl m_baseUrl;
};
#endif
// non QT includes
#include "PrintableTableView.h"
#include "mainWindow.h"
PrintableTableView::PrintableTableView( QWidget * parent ): QTableView(parent){
m_webview = new QWebView(this);
m_webview->setVisible(false);
}
PrintableTableView::~PrintableTableView(){
}
void
PrintableTableView::print() {
QString html;
MainWindow *mainWindow = MainWindow::instance();
// Header
html= QString("\
<div id=header>\
<div id=logo>\
<table border=0>\
<tr>\
<th><h1>Empty page</h1></th>\
<th><h2>from %1</h2></th>\
</tr>\
</table>\
<h3>printed on %2</h3>\
</div>\
</div>").arg(mainWindow->currentProjectName()).arg(QDate::currentDate().toS tring());
m_htmlTemplate = readFile(QLatin1String(":/identifiers.html"));
m_htmlTemplate.replace(QLatin1String("<!-- CONTENT -->"), html);
m_baseUrl = QUrl(QLatin1String("qrc:/identifiers.html"));
connect(m_webview, SIGNAL(loadFinished(bool)),this, SLOT(printHtml(bool)));
m_webview->setHtml(m_htmlTemplate, m_baseUrl);
}
void
PrintableTableView::printHtml(bool ok) {
disconnect(m_webview, SIGNAL(loadFinished(bool)),this, SLOT(printHtml(bool)));
QPrinter printer;
QPrintDialog printDialog(&printer, this);
printDialog.setOption(QAbstractPrintDialog::PrintS election,false);
printDialog.setOption(QAbstractPrintDialog::PrintP ageRange,false);
if (printDialog.exec() == QDialog::Accepted) {
m_webview->print(&printer);
}
}
QString
PrintableTableView::readFile(const QString &name)
{
QFile f(name);
if (!f.open(QIODevice::ReadOnly)) {
qWarning("Unable to open %s: %s", name.toUtf8().constData(), f.errorString().toUtf8().constData());
return QString();
}
QTextStream ts(&f);
return ts.readAll();
}
The content of the QTableView is displayed correctly before and while I have the printDialog open. Sometimes (yes sometimes, not everytime) it happens that the content of the QTableView disapear if I cancel the printDialog.
What I am doing wrong ?