#include <QtGui>

class GView : public QGraphicsView
{
protected:
    void paintEvent(QPaintEvent *event)
    {
        static QTime time;
        static QTime rt;
        time.restart();
        QGraphicsView::paintEvent(event);
        qDebug("%d %d", time.elapsed(), rt.restart());
    }
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QGraphicsScene scene;
    QGraphicsRectItem *root = scene.addRect(QRect(0, 0, 1000, 1000), QPen(), QBrush(Qt::lightGray));
    root->setFlag(QGraphicsItem::ItemClipsChildrenToShape);

    for (int i = 0; i < 100; ++i) {
        QGraphicsEllipseItem *ellipse = scene.addEllipse(QRect(0, 0, 200, 200), QPen(),
                                                         QBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)));
        ellipse->setPos(-100 + qrand() % 1000, -100 + qrand() % 1000);
        ellipse->setFlag(QGraphicsItem::ItemIsMovable);
        ellipse->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
        ellipse->setParentItem(root);

        for (int j = 0; j < 50; ++j) {
            QGraphicsEllipseItem *ellipse2 = scene.addEllipse(QRect(0, 0, 50, 50), QPen(),
                                                         QBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)));
            ellipse2->setPos(-25 + qrand() % 200, -25 + qrand() % 200);
            ellipse2->setFlag(QGraphicsItem::ItemIsMovable);
            ellipse2->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
            ellipse2->setParentItem(ellipse);

            for (int k = 0; k < 10; ++k) {
                QGraphicsRectItem *rect = scene.addRect(QRect(0, 0, 8, 8), QPen(),
                                                                  QBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)));
                rect->setPos(-4 + qrand() % 50, -4 + qrand() % 50);
                rect->setFlag(QGraphicsItem::ItemIsMovable);
                rect->setParentItem(ellipse2);
            }
        }
    }

    GView view;
    view.setScene(&scene);
    view.show();

    return app.exec();
}

