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:
Code:
QPaintEngine::setSystemClip: Should not be changed
while engine is active
QPaintEngine::setSystemRect: Should not be changed
while engine is active
and the application seems not responding to commands.
Thanks for the help.
Here the code:
viewer.h
Code:
#ifndef VIEWER_H
#define VIEWER_H
{
public:
protected:
};
#endif
viewer.cpp
Code:
#include <QtGui>
#include "viewer.h"
{
setStyleSheet("background-color: white");
}
{
doc.setHtml("<strong>What's wrong</strong> with <em>this code?</em>");
doc.
setPageSize(QSize(250,
300));
painter->translate(10, 10);
doc.documentLayout()->draw(painter, context);
}
main.cpp
Code:
#include <QtGui>
#include "viewer.h"
int main(int argc, char *argv[])
{
window->setWindowTitle("Painter & QTextDocument");
htmlTextEdit->setMaximumHeight(150);
htmlTextEdit->setMinimumHeight(150);
Viewer *viewer = new Viewer;
viewer->setMinimumHeight(150);
viewer->setMinimumWidth(100);
labelLayout->addWidget(htmlStringLabel);
labelLayout->addStretch();
buttonLayout->addStretch();
buttonLayout->addWidget(drawButton);
buttonLayout->addStretch();
mainLayout->addLayout(labelLayout);
mainLayout->addWidget(htmlTextEdit);
mainLayout->addLayout(buttonLayout);
mainLayout->addWidget(viewer);
mainLayout->setMargin(2);
mainLayout->setSpacing(0);
window->setLayout(mainLayout);
window->resize(400,400);
window->show();
return app.exec();
}
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
Re: QPaintEngine::setSystemClip: Should not be changed...
As usual quick and efficient!
Thank you marcel, delete painter; does the job.
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
Re: QPaintEngine::setSystemClip: Should not be changed...
Well, you'll have to post an example that reproduces that...
Regards
Re: QPaintEngine::setSystemClip: Should not be changed...
Here I am
and here a sample code with the problem (only implementation):
viewer.cpp
Code:
#include <QtGui>
#include "viewer.h"
{
setStyleSheet("background-color: white");
}
{
doc.setHtml(htmlString);
doc.
setPageSize(QSize(250,
300));
painter->translate(10, 10);
doc.documentLayout()->draw(painter, context);
delete painter;
}
void Viewer
::setHtmlString(QString string
) {
htmlString = string;
}
dialog.cpp
Code:
#include <QtGui>
#include "dialog.h"
{
htmlStringLabel
= new QLabel("HTML code:");
htmlTextEdit->setMaximumHeight(150);
htmlTextEdit->setMinimumHeight(150);
viewer = new Viewer;
viewer->setMinimumHeight(150);
viewer->setMinimumWidth(100);
labelLayout->addWidget(htmlStringLabel);
labelLayout->addStretch();
buttonLayout->addStretch();
buttonLayout->addWidget(drawButton);
buttonLayout->addStretch();
mainLayout->addLayout(labelLayout);
mainLayout->addWidget(htmlTextEdit);
mainLayout->addLayout(buttonLayout);
mainLayout->addWidget(viewer);
mainLayout->setMargin(2);
mainLayout->setSpacing(0);
setLayout(mainLayout);
resize(400,400);
connect(drawButton, SIGNAL(clicked()), this, SLOT(drawHtmlString()));
}
void Dialog::drawHtmlString()
{
viewer->setHtmlString(htmlTextEdit->toPlainText());
}
After pushing the button nothing changes in painting area; only if I move the mouse over it the new text is painted.
Bye
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
Re: QPaintEngine::setSystemClip: Should not be changed...
Again a simple and useful hint by marcel(TM) ;).
Thanks again