Results 1 to 3 of 3

Thread: Problem with QPrintPreviewWidget

  1. #1
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with QPrintPreviewWidget

    The difference between the following codes are indicated by comments, ignore the layout arrangement.
    This crashes to: "Access violation reading location 0xcdcdcdd1." in QPicture::play(QPainter *painter), the picture is null.
    Qt Code:
    1. QPrintPreviewWidget *testPreview_1 = new QPrintPreviewWidget();
    2. QPrintPreviewWidget *testPreview_2 = new QPrintPreviewWidget();
    3. QPushButton *btn_1 = new QPushButton("zoomIn_1");
    4. QPushButton *btn_2 = new QPushButton("zoomIn_2");
    5. //doing layouts
    6.  
    7. connect(testPreview_2, SIGNAL(paintRequested(QPrinter*)),
    8. this, SLOT(testSlot(QPrinter*)));
    9. connect(testPreview_1, SIGNAL(paintRequested(QPrinter*)),
    10. this, SLOT(testSlot(QPrinter*)));
    To copy to clipboard, switch view to plain text mode 

    I removed the second connection to render printer's contents, and it works well
    Qt Code:
    1. QPrintPreviewWidget *testPreview_1 = new QPrintPreviewWidget();
    2. QPrintPreviewWidget *testPreview_2 = new QPrintPreviewWidget();
    3. QPushButton *btn_1 = new QPushButton("zoomIn_1");
    4. QPushButton *btn_2 = new QPushButton("zoomIn_2");
    5. //doing layouts
    6.  
    7. connect(testPreview_2, SIGNAL(paintRequested(QPrinter*)),
    8. this, SLOT(testSlot(QPrinter*)));
    9. // connect(testPreview_1, SIGNAL(paintRequested(QPrinter*)),
    10. // this, SLOT(testSlot(QPrinter*)));
    To copy to clipboard, switch view to plain text mode 

    Or else, i removed the buttons, leave 2 print preview widget only, and it works well, too. 2 QPrintpreviewWidget are shown side by side.
    Qt Code:
    1. QPrintPreviewWidget *testPreview_1 = new QPrintPreviewWidget();
    2. QPrintPreviewWidget *testPreview_2 = new QPrintPreviewWidget();
    3. // QPushButton *btn_1 = new QPushButton("zoomIn_1");
    4. // QPushButton *btn_2 = new QPushButton("zoomIn_2");
    5. //doing layouts
    6.  
    7. connect(testPreview_2, SIGNAL(paintRequested(QPrinter*)),
    8. this, SLOT(testSlot(QPrinter*)));
    9. connect(testPreview_1, SIGNAL(paintRequested(QPrinter*)),
    10. this, SLOT(testSlot(QPrinter*)));
    To copy to clipboard, switch view to plain text mode 

    Who has ever came across this problem? is this a but of Qt?

  2. #2
    Join Date
    Sep 2009
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QPrintPreviewWidget

    Same problem here, imo problem appears, when you are drawining "parallel" into two QPrintPreviewWidget. I took a short look inside QPrintPreviewWidget and magic buildin QPrinter::setPreviewMode etc.. and it is really hacked (QPicture). You have to make sure, that you are using only one QPrintPreviewWidget in same time. If it is for small page range, for example 1..5 its no problem in 99.9%, but when I needed to preview a lot of pages (about 150 probably), the painting crashed. If you really need to use 2 QPrintPreviewWidget in "parallel", use QWidget::setUpdatesEnabled(false) to control painting times, it resolved that problem for me.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with QPrintPreviewWidget

    Please provide a self-contained, compilable example that crashes. The code below has been running for an hour, zooming and moving every second in two previews of 150 pages, without issue on my machine (Linux Qt 4.7.4):
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class Widget: public QWidget {
    5. Q_OBJECT
    6. public:
    7. Widget(QWidget *p = 0): QWidget(p) {
    8. QFile file("lipsum.txt");
    9. if (file.open(QIODevice::ReadOnly)) {
    10. m_lipsum = file.readAll();
    11. }
    12.  
    13. m_preview1 = new QPrintPreviewWidget(this);
    14. m_preview2 = new QPrintPreviewWidget(this);
    15. QHBoxLayout *layout = new QHBoxLayout(this);
    16. layout->addWidget(m_preview1);
    17. layout->addWidget(m_preview2);
    18. setLayout(layout);
    19.  
    20. connect(m_preview1, SIGNAL(paintRequested(QPrinter*)), SLOT(doPaint(QPrinter*)));
    21. connect(m_preview2, SIGNAL(paintRequested(QPrinter*)), SLOT(doPaint(QPrinter*)));
    22.  
    23. // Now make something happen routinely
    24. QTimer *timer = new QTimer(this);
    25. connect(timer, SIGNAL(timeout()), SLOT(doStuff()));
    26. timer->start(1000);
    27. }
    28.  
    29.  
    30. public slots:
    31. void doPaint(QPrinter *printer) {
    32. qDebug() << Q_FUNC_INFO << sender();
    33. static const QFont small("Arial", 10);
    34. static const QFont large("Arial", 32);
    35. if (p.begin(printer)) {
    36. for (int i = 0; i < 150; ++i) {
    37. p.save();
    38. p.setFont(large);
    39. p.drawText(100, 100, QString::number(i));
    40. p.translate(0, 200);
    41. p.setFont(small);
    42. p.drawText(p.window(), m_lipsum);
    43. printer->newPage();
    44. p.restore();
    45. }
    46. }
    47. }
    48.  
    49. void doStuff() {
    50. static int count = 0;
    51. m_preview1->setZoomFactor(0.5 + (count % 7) * 0.1);
    52. m_preview1->setCurrentPage(count % 23);
    53. m_preview2->setZoomFactor( 0.5 + (count % 13) * 0.1);
    54. m_preview2->setCurrentPage(count % 19);
    55. ++count;
    56. }
    57.  
    58. private:
    59. QString m_lipsum;
    60. QPrintPreviewWidget *m_preview1;
    61. QPrintPreviewWidget *m_preview2;
    62. };
    63.  
    64. int main(int argc, char *argv[])
    65. {
    66. QApplication app(argc, argv);
    67.  
    68. Widget w;
    69. w.show();
    70. return app.exec();
    71. }
    72. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    lipsum.txt is 5 fine paragraphs of gibberish from http://www.lipsum.com
    Painting is requested only once for each preview.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 12:05
  2. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  3. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  4. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.