[Qt-interest] (QWidget *) arriving through QGraphicsItem::paint(...) is not the (QGraphicsView *) expected

Sherif Ghali sherif.ghali.1 at gmail.com
Sat Jul 16 19:38:19 CEST 2011


On Sat, Jul 16, 2011 at 3:38 AM, Anton Chernov <mechernov at gmail.com> wrote:
> http://doc.qt.nokia.com/latest/qgraphicsscene.html#views
>
> the rest you can choose - either setting an object name
>
> http://doc.qt.nokia.com/latest/qobject.html#objectName-prop
>
> or a map of pointers, or just the index...
>

I am not just looking for the list of views currently linked to a scene.
I would like each graphicsitem to know the graphicsview on which it is
being painted.

Let me ask the question within a toy example.

We create two QGraphicsViews and set the 'bool diagonal' member
variable to false in one and to true in the other. Each QGraphicsView
displays a QGraphicsItem, and each QGraphicsItem asks its respective
QGraphicsView whether the rectangle it paints should have a diagonal
across. We should obtain one rectangle with a diagonal across and the
other without. We should also not receive messages on the console
saying that a dynamic_cast has failed.

// main.cpp
#include "MyGraphicsView.h"
#include <QtGui>

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);
    QGraphicsScene * scene = new QGraphicsScene;
    MyGraphicsView view1(scene, true);
    MyGraphicsView view2(scene, false);
    view1.show();
    view2.show();
    return app.exec();
}

// MyGraphicsView.h
#include <QtGui>

class MyGraphicsView : public QGraphicsView
{
public:
    MyGraphicsView(QGraphicsScene * scene, bool s);

    bool diagonal() const { return _diagonal; }

protected:
    bool _diagonal;
};

// MyGraphicsView.cpp
#include "MyGraphicsView.h"
#include "MyGraphicsItem.h"

MyGraphicsView::MyGraphicsView(QGraphicsScene * myscene, bool s)
    : QGraphicsView(myscene), _diagonal(s)
{
    scene()->addItem( new MyGraphicsItem(QRectF(QPointF(10,10),
QPointF(50,40))) );
}

// MyGraphicsItem.h
#include <QtGui>
#include <iostream>

class MyGraphicsItem : public QGraphicsItem
{
public:
    MyGraphicsItem(const QRectF & r, QGraphicsItem * parent = 0)
        : QGraphicsItem(parent), rect(r)
    {}
protected:
    virtual QRectF boundingRect() const { return rect; }

    virtual void paint(QPainter * painter,
                       const QStyleOptionGraphicsItem *,
                       QWidget * widget = 0)
    {
        MyGraphicsView * mgw = dynamic_cast<MyGraphicsView *>(widget);

        if(!mgw)
            std::cout << "dynamic_cast failed: "
                "Why is MyGraphicsItem::paint(..) not "
                "receiving a MyGraphicsView pointer?"
                      << std::endl;
        else
            if(mgw->diagonal())
                painter->drawLine(rect.bottomLeft(), rect.topRight());
        painter->drawRect(rect);
    }

    QRectF rect;
};

# project file
CONFIG += console
mac { CONFIG -= app_bundle }

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

HEADERS += MyGraphicsItem.h MyGraphicsView.h
SOURCES += MyGraphicsView.cpp main.cpp



More information about the Qt-interest-old mailing list