So you'd like your QGraphicsView contents to shine through into your widget, and you also want your widget to be able to redraw itself without redrawing the QGV. So if the QGV changes under your widget, then I assume you want the widget to recompose itself on top of the QGV, right?
Have you tried QGraphicsView::render()? Try this. Enable the OpaquePaintEvent flag on your widget. Then call QGraphicsView::render() and then draw your widget contents. Like this (untested code):
{
graphicsView->render(rect(), &painter, geometry());
... do your painting here
}
void OverlayWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
graphicsView->render(rect(), &painter, geometry());
... do your painting here
}
To copy to clipboard, switch view to plain text mode
The render function will draw what's under your widget into your widget. So you're doing your own composition. You can also save your widget's background into a QPixmap to avoid asking QGraphicsView to render it all the time. The QGraphicsView, on its side, can do something like this:
void CustomGraphicsView
::paintEvent(QPaintEvent *event
) {
if (exposed.intersects(overlayWidget.geometry())) {
QPixmap pixmap
(overlayWidget.
size());
pixmap.fill(0);
render(overlayWidget.rect(), &pixmapPainter, overlayWidget.geometry());
overlayWidget->updateBackground(pixmap);
}
}
void CustomGraphicsView::paintEvent(QPaintEvent *event)
{
QRegion exposed = event->region();
if (exposed.intersects(overlayWidget.geometry())) {
QPixmap pixmap(overlayWidget.size());
pixmap.fill(0);
QPainter pixmapPainter(&pixmap);
render(overlayWidget.rect(), &pixmapPainter, overlayWidget.geometry());
overlayWidget->updateBackground(pixmap);
}
QGraphicsView::paintEvent(event);
}
To copy to clipboard, switch view to plain text mode
The idea being that the view updates the overlaywidget's background, so that the overlay can rerender it without asking the view all the time, and the view basically "pushes" changes to the overlay only when necessary....
Bookmarks