Results 1 to 8 of 8

Thread: QPaintEngine::setSystemClip: Should not be changed...

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QPaintEngine::setSystemClip: Should not be changed...

    Hi, I have a problem with my new sample application; I was reading QTextDocument manual and as exercise I wrote an application very simple:

    - a textEdit where insert html code
    - a button for passing the textEdit->text() to...
    - a widget that reimplement paintEvent() so it uses textDocument->setHtml() to render text.

    Application compiles right (even if I not wrote the connection); but during execution console fills with these two lines:

    Qt Code:
    1. QPaintEngine::setSystemClip: Should not be changed while engine is active
    2. QPaintEngine::setSystemRect: Should not be changed while engine is active
    To copy to clipboard, switch view to plain text mode 

    and the application seems not responding to commands.

    Thanks for the help.

    Here the code:

    viewer.h
    Qt Code:
    1. #ifndef VIEWER_H
    2. #define VIEWER_H
    3.  
    4. class QPainter;
    5.  
    6. class Viewer: public QWidget
    7. {
    8. public:
    9. Viewer(QWidget *parent = 0);
    10.  
    11. protected:
    12. void paintEvent(QPaintEvent *event);
    13. };
    14.  
    15. #endif
    To copy to clipboard, switch view to plain text mode 

    viewer.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "viewer.h"
    4.  
    5. Viewer::Viewer(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. setStyleSheet("background-color: white");
    9. }
    10.  
    11.  
    12. void Viewer::paintEvent(QPaintEvent * /* event */)
    13. {
    14. QPainter *painter = new QPainter(this);
    15. doc.setHtml("<strong>What's wrong</strong> with <em>this code?</em>");
    16. QAbstractTextDocumentLayout::PaintContext context;
    17. doc.setPageSize(QSize(250,300));
    18. painter->translate(10, 10);
    19. doc.documentLayout()->draw(painter, context);
    20. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "viewer.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. QWidget *window = new QWidget;
    9. window->setWindowTitle("Painter & QTextDocument");
    10.  
    11. QLabel *htmlStringLabel = new QLabel("HTML code:");
    12. QTextEdit *htmlTextEdit = new QTextEdit;
    13. htmlTextEdit->setMaximumHeight(150);
    14. htmlTextEdit->setMinimumHeight(150);
    15. QPushButton *drawButton = new QPushButton("&Draw");
    16. Viewer *viewer = new Viewer;
    17. viewer->setMinimumHeight(150);
    18. viewer->setMinimumWidth(100);
    19.  
    20. QHBoxLayout *labelLayout = new QHBoxLayout;
    21. labelLayout->addWidget(htmlStringLabel);
    22. labelLayout->addStretch();
    23.  
    24. QHBoxLayout *buttonLayout = new QHBoxLayout;
    25. buttonLayout->addStretch();
    26. buttonLayout->addWidget(drawButton);
    27. buttonLayout->addStretch();
    28.  
    29. QVBoxLayout *mainLayout = new QVBoxLayout;
    30. mainLayout->addLayout(labelLayout);
    31. mainLayout->addWidget(htmlTextEdit);
    32. mainLayout->addLayout(buttonLayout);
    33. mainLayout->addWidget(viewer);
    34. mainLayout->setMargin(2);
    35. mainLayout->setSpacing(0);
    36.  
    37. window->setLayout(mainLayout);
    38. window->resize(400,400);
    39. window->show();
    40.  
    41. return app.exec();
    42. }
    To copy to clipboard, switch view to plain text mode 
    Giuseppe CalÃ

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPaintEngine::setSystemClip: Should not be changed...

    That is because in paintEvent you create a painter on the heap and you don't delete at the end of the function, meaning the paint device(the widget) remains active.
    When the next paint event occurs, you get the warning, since a paint was already in progress.

    So, call delete painter; at the end of the paint event.

    Another thing: it is not a good idea to create a text doc in every paint event.
    Instead create it as a member.

    Regards

  3. The following user says thank you to marcel for this useful post:

    jiveaxe (8th August 2007)

  4. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPaintEngine::setSystemClip: Should not be changed...

    As usual quick and efficient!
    Thank you marcel, delete painter; does the job.
    Giuseppe CalÃ

  5. #4
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPaintEngine::setSystemClip: Should not be changed...

    One more question. Why with my some applications with reimplemented paintEvent() after the paint event the paint area is not updated if not moving the mouse?

    Regards
    Giuseppe CalÃ

  6. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPaintEngine::setSystemClip: Should not be changed...

    Well, you'll have to post an example that reproduces that...

    Regards

  7. #6
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPaintEngine::setSystemClip: Should not be changed...

    Here I am
    and here a sample code with the problem (only implementation):

    viewer.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "viewer.h"
    4.  
    5. Viewer::Viewer(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. setStyleSheet("background-color: white");
    9. }
    10.  
    11. void Viewer::paintEvent(QPaintEvent * /* event */)
    12. {
    13. QPainter *painter = new QPainter(this);
    14. doc.setHtml(htmlString);
    15. QAbstractTextDocumentLayout::PaintContext context;
    16. doc.setPageSize(QSize(250,300));
    17. painter->translate(10, 10);
    18. doc.documentLayout()->draw(painter, context);
    19. delete painter;
    20. }
    21.  
    22. void Viewer::setHtmlString(QString string)
    23. {
    24. htmlString = string;
    25. }
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "dialog.h"
    4.  
    5. Dialog::Dialog(QWidget *parent)
    6. :QDialog(parent)
    7. {
    8. htmlStringLabel = new QLabel("HTML code:");
    9. htmlTextEdit = new QTextEdit;
    10. htmlTextEdit->setMaximumHeight(150);
    11. htmlTextEdit->setMinimumHeight(150);
    12. drawButton = new QPushButton("&Draw");
    13. viewer = new Viewer;
    14. viewer->setMinimumHeight(150);
    15. viewer->setMinimumWidth(100);
    16.  
    17. QHBoxLayout *labelLayout = new QHBoxLayout;
    18. labelLayout->addWidget(htmlStringLabel);
    19. labelLayout->addStretch();
    20.  
    21. QHBoxLayout *buttonLayout = new QHBoxLayout;
    22. buttonLayout->addStretch();
    23. buttonLayout->addWidget(drawButton);
    24. buttonLayout->addStretch();
    25.  
    26. QVBoxLayout *mainLayout = new QVBoxLayout;
    27. mainLayout->addLayout(labelLayout);
    28. mainLayout->addWidget(htmlTextEdit);
    29. mainLayout->addLayout(buttonLayout);
    30. mainLayout->addWidget(viewer);
    31. mainLayout->setMargin(2);
    32. mainLayout->setSpacing(0);
    33.  
    34. setLayout(mainLayout);
    35. resize(400,400);
    36.  
    37. connect(drawButton, SIGNAL(clicked()), this, SLOT(drawHtmlString()));
    38. }
    39.  
    40. void Dialog::drawHtmlString()
    41. {
    42. viewer->setHtmlString(htmlTextEdit->toPlainText());
    43. }
    To copy to clipboard, switch view to plain text mode 

    After pushing the button nothing changes in painting area; only if I move the mouse over it the new text is painted.

    Bye
    Giuseppe CalÃ

  8. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPaintEngine::setSystemClip: Should not be changed...

    You need to schedule a viewer repaint by calling its update() merthod.
    You can do this either in Dialog::drawHtmlString by calling viewer->update() after setting the html, or in Viewer::setHtmlString, after assigning the new string, by calling update();

    Regards

  9. #8
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPaintEngine::setSystemClip: Should not be changed...

    Again a simple and useful hint by marcel(TM) .

    Thanks again
    Giuseppe CalÃ

Similar Threads

  1. Splitter Color changed when referesh?
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 11th July 2007, 08:10
  2. Informing a model its data has changed
    By saknopper in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 19:58
  3. Replies: 9
    Last Post: 23rd November 2006, 11:39
  4. Replies: 3
    Last Post: 2nd April 2006, 06:48
  5. QtSingleApplication
    By mule in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2006, 19:21

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.