PDA

View Full Version : QtableView content disapears after associated PrintDialog got cancelled



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 ?

wysota
24th July 2009, 21:17
How is the model for the view defined? Also, how do you instantiate your view?

schall_l
24th July 2009, 21:38
Here is how the model for the view is defined and instantiated.


class CommunicationModel : public QAbstractTableModel
{
Q_OBJECT

public:
static CommunicationModel* instance(QWidget* parent = 0);

int rowCount( const QModelIndex &index) const;
int columnCount( const QModelIndex &index) const;

QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags( const QModelIndex& ) const ;

bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex());
bool addFrame(const Can_Frame &frame);
void clear();

void sort (int column, Qt::SortOrder order = Qt::AscendingOrder );

private:
CommunicationModel(QObject *parent = 0);

IdentifiersModel* id_model;
QList<Can_Frame> m_communication; // Communication log

};



CommunicationView::CommunicationView( QWidget* parent ) :
PrintableTableView(parent)
{
m_model = CommunicationModel::instance(this);
setModel(m_model);

m_delegate = new CommunicationDelegate(this);
setItemDelegate(m_delegate);

.. and how I do instantiate my view ...


WidgetCommunication::WidgetCommunication( QWidget* parent ) :
QWidget( parent )
{

// Title
title = new TitleFrame(this,"Communication log",true);
// Body
body = CommunicationView::instance(this);
// Status
quickSend = new QuickSend(this);

// Layout
mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(title);
mainLayout->addWidget(body);
mainLayout->addWidget(quickSend);

mainLayout->setContentsMargins (0, 0, 0, 0);
setLayout(mainLayout);
}

wysota
24th July 2009, 21:57
And what's the relation between WidgetCommunication and the print dialog?

schall_l
24th July 2009, 22:13
??..Well PrintDialog is a child of QtableView...


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);
}
}

wysota
24th July 2009, 22:30
Could you install an event filter on the table and see if it receives a QEvent::Hide?