Hello there
I am trying to visualize a cartesian 2D-plane with x/y axis and about one milion dots (QRectF).
void myWidget::setupView()
{
scene.setSceneRect(0,0,100,100);
scene.
setBackgroundBrush(QColor(255,
255,
0,
255));
drawAxis();
for(long int i = 0;i<100000;i++)
{
item = scene.addRect(p,pen,brush);
item
->setFlag
(QGraphicsItem::ItemDoesntPropagateOpacityToChildren);
}
view.setScene(&scene);
view.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
view.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
//view.setResizeAnchor(QGraphicsView::AnchorViewCenter);
view.
setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
view.viewport()->setFocusProxy( this );
l->addWidget(&view);
l->setMargin(0);
l->setSpacing(0);
setLayout(l);
view.show();
}
{
//the view should show the whole scene at all times
QTransform t;
view.setTransform(t.scale(qreal(view.width()) / qreal(scene.sceneRect().width()), qreal(view.height()) / qreal(scene.sceneRect().height())));
}
void myWidget::setupView()
{
scene.setSceneRect(0,0,100,100);
scene.setBackgroundBrush(QColor(255,255,0,255));
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
QRectF p(40,40,0.1,0.1);
QBrush brush(Qt::red);
QPen pen(Qt::red);
drawAxis();
QGraphicsRectItem *item;
for(long int i = 0;i<100000;i++)
{
item = scene.addRect(p,pen,brush);
item->setFlag(QGraphicsItem::ItemDoesntPropagateOpacityToChildren);
}
view.setScene(&scene);
view.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
view.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
//view.setResizeAnchor(QGraphicsView::AnchorViewCenter);
view.setCacheMode(QGraphicsView::CacheBackground);
view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
view.setOptimizationFlag(QGraphicsView::DontClipPainter);
view.viewport()->setFocusProxy( this );
QVBoxLayout *l = new QVBoxLayout();
l->addWidget(&view);
l->setMargin(0);
l->setSpacing(0);
setLayout(l);
view.show();
}
void myWidget::resizeEvent(QResizeEvent *e)
{
//the view should show the whole scene at all times
QTransform t;
view.setTransform(t.scale(qreal(view.width()) / qreal(scene.sceneRect().width()), qreal(view.height()) / qreal(scene.sceneRect().height())));
QWidget::resizeEvent(e);
}
To copy to clipboard, switch view to plain text mode
now this is way too slow. painting takes ages and when myWidget gets resized, it takes 10 secs until the view sets up again. The view has to show the whole scene at all times, so each time the view gets resizes, its transformation needs to be set so it shows the whole scene again.
I have tried to prevent the QGraphicsItem:
aint to paint dots that dont need to be painted coz they are obscured:
{
painter->setClipRect( option->exposedRect );
}
void MyGraphicsItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget* w)
{
painter->setClipRect( option->exposedRect );
QGraphicsRectItem::paint( painter, option, w);
}
To copy to clipboard, switch view to plain text mode
it still is too slow. I have experimented with almost all flags in QGraphicsView (viewportUpdateMode, CacheMode,....) but it didnt help.
Does anyone have a simple example (code snippet) with a scene that contains one million items and its view still paints and reacts to resizing events as fast as it is praised on the qt-website?
Bookmarks