Results 1 to 6 of 6

Thread: QtableView content disapears after associated PrintDialog got cancelled

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default QtableView content disapears after associated PrintDialog got cancelled

    Hi,

    My PrintableTableView class deriving from QTableView is using QWebView to print the content of my model:

    Qt Code:
    1. #ifndef PRINTABLETABLEVIEW_H
    2. #define PRINTABLETABLEVIEW_H
    3.  
    4. // QT includes
    5. #include <QtWebKit>
    6. #include <QtGui>
    7.  
    8. // non QT includes
    9.  
    10. class PrintableTableView : public QTableView {
    11. Q_OBJECT
    12.  
    13. public:
    14. PrintableTableView ( QWidget * parent = 0 );
    15. ~PrintableTableView();
    16.  
    17. public slots:
    18. virtual void print();
    19. void printHtml(bool ok);
    20.  
    21. protected:
    22. // Webkit things for printing the identifiers
    23. QString readFile(const QString &name);
    24. QWebView *m_webview;
    25. QString m_htmlTemplate;
    26. QUrl m_baseUrl;
    27. };
    28.  
    29. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // non QT includes
    2. #include "PrintableTableView.h"
    3. #include "mainWindow.h"
    4.  
    5. PrintableTableView::PrintableTableView( QWidget * parent ): QTableView(parent){
    6. m_webview = new QWebView(this);
    7. m_webview->setVisible(false);
    8. }
    9.  
    10. PrintableTableView::~PrintableTableView(){
    11. }
    12.  
    13. void
    14. PrintableTableView::print() {
    15. QString html;
    16. MainWindow *mainWindow = MainWindow::instance();
    17.  
    18. // Header
    19. html= QString("\
    20. <div id=header>\
    21. <div id=logo>\
    22. <table border=0>\
    23. <tr>\
    24. <th><h1>Empty page</h1></th>\
    25. <th><h2>from %1</h2></th>\
    26. </tr>\
    27. </table>\
    28. <h3>printed on %2</h3>\
    29. </div>\
    30. </div>").arg(mainWindow->currentProjectName()).arg(QDate::currentDate().toString());
    31.  
    32. m_htmlTemplate = readFile(QLatin1String(":/identifiers.html"));
    33. m_htmlTemplate.replace(QLatin1String("<!-- CONTENT -->"), html);
    34. m_baseUrl = QUrl(QLatin1String("qrc:/identifiers.html"));
    35. connect(m_webview, SIGNAL(loadFinished(bool)),this, SLOT(printHtml(bool)));
    36. m_webview->setHtml(m_htmlTemplate, m_baseUrl);
    37. }
    38.  
    39. void
    40. PrintableTableView::printHtml(bool ok) {
    41. disconnect(m_webview, SIGNAL(loadFinished(bool)),this, SLOT(printHtml(bool)));
    42. QPrinter printer;
    43. QPrintDialog printDialog(&printer, this);
    44. printDialog.setOption(QAbstractPrintDialog::PrintSelection,false);
    45. printDialog.setOption(QAbstractPrintDialog::PrintPageRange,false);
    46. if (printDialog.exec() == QDialog::Accepted) {
    47. m_webview->print(&printer);
    48. }
    49. }
    50.  
    51. PrintableTableView::readFile(const QString &name)
    52. {
    53. QFile f(name);
    54. if (!f.open(QIODevice::ReadOnly)) {
    55. qWarning("Unable to open %s: %s", name.toUtf8().constData(), f.errorString().toUtf8().constData());
    56. return QString();
    57. }
    58. QTextStream ts(&f);
    59. return ts.readAll();
    60. }
    To copy to clipboard, switch view to plain text mode 

    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 ?
    Attached Images Attached Images

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.