4

I am trying to have a widget live freely inside a large scrollable panel. When I scroll around the QGraphics view, the child widget does not draw properly— its sub-rectangles will blank out, and the borders (which are rounded) are not blended with the canvas. I have:

QApplication qapp(argc, argv);
QGraphicsView  view;
QGraphicsScene scene;

// a simple QWidget with a gridlayout and some child objects:
MyQWidget mywidget(NULL); 

scene.addWidget(&mywidget);

view.setScene(&scene);
view.setSceneRect(0,0,1280,1280);
view.show();
view.resize(512,512);

qapp.exec();

I get the same bogus rendering if I use a QGraphicsProxyWidget and scene.addItem() instead.

If instead I parent my custom widget to the QGraphicsView, it renders properly, but then the widget does not seem to be part of the QGraphicsScene, because it doesn't scroll around anymore and remains fixed to the parent window.

What's going on?

Edit: Images of the blanking problem, with MyWidget replaced by QPushButton:

Before scrolling:
Before scrolling

Scrolling a little down and to the right:
Scrolling a little down and to the right.

Second edit: I have a Retina display. Could this be related? Is there special setup for Retina with Qt?

trbabb
  • 1,894
  • 18
  • 35

1 Answers1

0

You should be getting an error when you try to add a widget to the scene which already has a parent:

QGraphicsProxyWidget::setWidget: cannot embed widget which is not a toplevel widget, and is not a child of an embedded widget

QGraphicsScene::addWidget() is a wrapper around QGraphicsProxyWidget::setWidget which documentation says:

widget must be a top-level widget whose parent is 0.

To have borders blended you need to set widget to be transparent:

mywidget.setAttribute(Qt::WA_TranslucentBackground, true);
svlasov
  • 9,923
  • 2
  • 38
  • 39
  • Thanks, but that doesn't explain the graphics errors (blanking sub-rectangles). As you can see in the code above, my widget's parent IS null. Also, I can't set the background to transparent because it's a solid color, and I don't want the whole widget to be see-through. – trbabb Mar 14 '15 at 21:26
  • You can also use `setAttribute(Qt::WA_TranslucentBackground, true)`. Regarding the blanking, I couldn't reproduce that. Post some implementation. – svlasov Mar 14 '15 at 21:32
  • Try `setContentsMargins(0, 0, 0, 0)` – svlasov Mar 14 '15 at 21:52
  • Try also `view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);` – svlasov Mar 14 '15 at 21:59