You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

bnilsson

Beginner

  • "bnilsson" is male
  • "bnilsson" started this thread

Posts: 37

Location: Sweden

  • Send private message

1

Sunday, December 16th 2007, 5:13pm

How can I subclass QGraphicsScene;;drawItems?

I need to subclass QGraphicsScene;;drawItems.
It refers to QGraphicsScenePrivate, which is private to QGraphicsScene, so I am not allowed to subclass.

Can someone advice me how to do it?
MacOSX user

2

Sunday, December 16th 2007, 5:31pm

RE: How can I subclass QGraphicsScene;;drawItems?

you subclass QGraphicsScene
you reimplement drawItems

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class GraphicsScene : public QGraphicsScene
{
    GraphicsScene( QObject * p = NULL );
    ~GraphicsScene();
protected:
    void drawItems( QPainter * painter, int nbItem, QGraphicsItem *[] items, const QStyleOptionGraphicsItem[] options, QWidget * widget = NULL );
};

GraphicsScene::GraphicsScene( QObject * p = NULL ) : QGraphicsScene ( p )
{
    // ...
}

GraphicsScene::~GraphicsScene()
{
    // ...
}

void GraphicsScene::drawItems( QPainter * painter, int nbItem, QGraphicsItem *[] items, const QStyleOptionGraphicsItem[] options, QWidget * widget = NULL )
{
    // ...
}

for each item, all what is necessary to draw is porvided in
- items
- options[i]
no need access to QGraphicsScenePrivate
Nicolas

This post has been edited 5 times, last edit by "Nicolas SOUCHON" (Dec 16th 2007, 5:40pm)


bnilsson

Beginner

  • "bnilsson" is male
  • "bnilsson" started this thread

Posts: 37

Location: Sweden

  • Send private message

3

Sunday, December 16th 2007, 5:54pm

Thanks.
However, I would like to start with the unmodified content of drawItems:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void QGraphicsScene::drawItems(QPainter *painter,
                               int numItems,
                               QGraphicsItem *items[],
                               const QStyleOptionGraphicsItem options[], QWidget *widget)
{
    Q_D(QGraphicsScene);

    // Detect if painter state protection is disabled.
    bool painterStateProtection = d->painterStateProtection(painter);
    QTransform oldTransform = painter->worldTransform();

    for (int i = 0; i < numItems; ++i) {
        // Save painter
        if (painterStateProtection)
            painter->save();

        QGraphicsItem *item = items[i];

        if (item->d_ptr->itemIsUntransformable()) {
            painter->setTransform(item->deviceTransform(painter->worldTransform()), false);
        } else {
            painter->setTransform(item->sceneTransform(), true);
        }

        if (item->flags() & QGraphicsItem::ItemClipsToShape)
            painter->setClipPath(item->shape(), Qt::IntersectClip);
        if (item->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren) {
            // Set a clip path on \a painter by walking up the parent item
            // chain of \a item, intersecting clip paths as long as the item's
            // ancestor clips children.
            QGraphicsItem *target = item->parentItem();
            do {
                painter->setClipPath(item->mapFromItem(target, target->shape()), Qt::IntersectClip);
            } while ((target->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren)
                     && (target = target->parentItem()));
        }

        item->paint(painter, &options[i], widget);

        // Restore painter
        if (painterStateProtection)
            painter->restore();
        else
            painter->setWorldTransform(oldTransform);
    }
}
and since Q_D expands into

Source code

1
#define Q_D(Class) Class##Private * const d = d_func()
I need access to QGraphicsScenePrivate, which I am not allowed to. The solution would be to subclass that too, but here I am stuck, just can't seem to get it right.
I have not yet insvstigated if I can code it in a diferent way to avoid the need of QGraphcsScenePrivate, maybe it is possible.
MacOSX user

4

Sunday, December 16th 2007, 6:51pm

as you can see QGraphicsScenePrivate is only used for painter saving and restoring

so I suggest you to try using:
painter->save()
and
painter->restore()
as if d->painterStateProtection(painter) was true

I don't have any solution to access the QGraphicsScenePrivate object

even if you subclass QGraphicsScenePrivate, the QGraphicsScenePrivate object of QGraphicsScene will still keep to be private
Nicolas

This post has been edited 1 times, last edit by "Nicolas SOUCHON" (Dec 16th 2007, 6:57pm)


bnilsson

Beginner

  • "bnilsson" is male
  • "bnilsson" started this thread

Posts: 37

Location: Sweden

  • Send private message

5

Sunday, December 16th 2007, 7:06pm

Thanks again, I tried it and it seems to work.
I was confused by all the transform-related stuff.
As I am not using any transforms in my app, it may be safe.

This is my code now:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
void myQGraphicsScene::drawItems(QPainter *painter,
                               int numItems,
                               QGraphicsItem *items[],
                               const QStyleOptionGraphicsItem options[], QWidget *widget)
{
    for (int i = 0; i < numItems; ++i) {
		painter->save();
        QGraphicsItem *item = items[i];
        item->paint(painter, &options[i], widget);
		painter->restore();

	}
}


Would this work, or am I living dangerously?
I am only using scene->addRect and scene->addPolygon, no transformations.
The reason I wanted access to drawItems is that the number of items I have is sometimes very large, so the application locks until the drawing is finished. I will try to give some time to the eventloop during this process.
MacOSX user

This post has been edited 1 times, last edit by "bnilsson" (Dec 16th 2007, 7:08pm)


6

Sunday, December 16th 2007, 7:48pm

the best is to try!
Nicolas