Results 1 to 6 of 6

Thread: QtableView content disapears after associated PrintDialog got cancelled

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

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtableView content disapears after associated PrintDialog got cancelled

    How is the model for the view defined? Also, how do you instantiate your view?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: QtableView content disapears after associated PrintDialog got cancelled

    Here is how the model for the view is defined and instantiated.

    Qt Code:
    1. class CommunicationModel : public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. static CommunicationModel* instance(QWidget* parent = 0);
    7.  
    8. int rowCount( const QModelIndex &index) const;
    9. int columnCount( const QModelIndex &index) const;
    10.  
    11. QVariant data(const QModelIndex &index, int role) const;
    12. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    13. Qt::ItemFlags flags( const QModelIndex& ) const ;
    14.  
    15. bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex());
    16. bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex());
    17. bool addFrame(const Can_Frame &frame);
    18. void clear();
    19.  
    20. void sort (int column, Qt::SortOrder order = Qt::AscendingOrder );
    21.  
    22. private:
    23. CommunicationModel(QObject *parent = 0);
    24.  
    25. IdentifiersModel* id_model;
    26. QList<Can_Frame> m_communication; // Communication log
    27.  
    28. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. CommunicationView::CommunicationView( QWidget* parent ) :
    2. PrintableTableView(parent)
    3. {
    4. m_model = CommunicationModel::instance(this);
    5. setModel(m_model);
    6.  
    7. m_delegate = new CommunicationDelegate(this);
    8. setItemDelegate(m_delegate);
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. WidgetCommunication::WidgetCommunication( QWidget* parent ) :
    2. QWidget( parent )
    3. {
    4.  
    5. // Title
    6. title = new TitleFrame(this,"Communication log",true);
    7. // Body
    8. body = CommunicationView::instance(this);
    9. // Status
    10. quickSend = new QuickSend(this);
    11.  
    12. // Layout
    13. mainLayout = new QVBoxLayout(this);
    14. mainLayout->addWidget(title);
    15. mainLayout->addWidget(body);
    16. mainLayout->addWidget(quickSend);
    17.  
    18. mainLayout->setContentsMargins (0, 0, 0, 0);
    19. setLayout(mainLayout);
    20. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtableView content disapears after associated PrintDialog got cancelled

    And what's the relation between WidgetCommunication and the print dialog?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: QtableView content disapears after associated PrintDialog got cancelled

    ??..Well PrintDialog is a child of QtableView...

    Qt Code:
    1. void
    2. PrintableTableView::printHtml(bool ok) {
    3. disconnect(m_webview, SIGNAL(loadFinished(bool)),this, SLOT(printHtml(bool)));
    4. QPrinter printer;
    5. QPrintDialog printDialog(&printer, this);
    6. printDialog.setOption(QAbstractPrintDialog::PrintSelection,false);
    7. printDialog.setOption(QAbstractPrintDialog::PrintPageRange,false);
    8. if (printDialog.exec() == QDialog::Accepted) {
    9. m_webview->print(&printer);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtableView content disapears after associated PrintDialog got cancelled

    Could you install an event filter on the table and see if it receives a QEvent::Hide?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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.