Qt Snippet: " Painter not active " error
The problem usually came from the use of the wrong QPaintDevice pointer. Some Qt widgets, like for example QGraphicsView, have an additional "internal" paint area usually called viewport that need to be used for paint instead of the main object. Translating this explanation into code we can use as example the subclass of the QGraphicsView as follow:
void MyGraphicsView::paintEvent(QPaintEvent * event) { QPainter painter(this); // Here draw the content }
This code will show up the "Painter not active" error because for this specific widget the internal viewport have to be used. The correct code will be the following:
void MyGraphicsView::paintEvent(QPaintEvent * event)
{
QPainter painter(viewport());
// Here draw the content
}
Through the call viewport() we get the working QPaintDevice pointer to use for paint widget area.
Comments
Post a Comment