[Qt-interest] Qt/C++ working with graphicsview.

Stephen Deetz stephendeetz at chibardun.net
Sun Jan 24 16:30:28 CET 2010


Trying to get used to Qt/C++ by working with graphicsview.

 

Main question: How does a scene connect to an item for mouse events?

 

Other questions:

1)  Am I using the inherited stuff correctly?

a.  Calling the super classes.

b.  Virtual keyword needed for subclass only if planning on making
descendents?

2)  I think I know what bounding rect is, but why is it needed if shape is
used for collisions?

3)  Qreal to int conversion. Is this a problem?

 

Would appreciate any help.

 

Cordially,

 

Stephen Deetz

 

------------ Source Code -----------

 

#include <QtGui>

 

class MyEllipse : public QGraphicsEllipseItem

{

private:

    qreal m_X;

    qreal m_Y;

    qreal m_W;

    qreal m_H;

 

public:

    MyEllipse(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = 0)

    {

        m_X = x;

        m_Y = y;

        m_W = w;

        m_H = h;

 

        QGraphicsEllipseItem::QGraphicsEllipseItem( m_X, m_Y, m_W, m_H,
parent ); //needed?

    }

 

    void paint( QPainter *painter,

                const QStyleOptionGraphicsItem *option,

                QWidget *widget )

    {

        QRadialGradient gradient(50, 50, 50, 30, 30);

        gradient.setColorAt(0.2, Qt::white);

        gradient.setColorAt(0.8, Qt::green);

        gradient.setColorAt(1, Qt::black);

        painter->setBrush(gradient);

        painter->drawEllipse( m_X, m_Y, m_W, m_H ); //Any problem with
qreal's passed as int's?

    };

 

    /*

    QRectF boundingRect() const //Does boundingRect or shape have to be
implemented for mousePressEvent?

    { //const int MARGIN=1;

 

      return this->boundingRect( ); //These coordinates would be related to
item (m_X, etc..) and not the scene, correct?

    };

    */

 

    //How does this get called?

    //The scene gets the mouse events and then automatically passes to
correct scene item?

    //Or does a connect need to be put somewhere?

    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) //Is
virtual keyword only needed if planning on more descendents?

    {

        QGraphicsItem::mousePressEvent(event);

        qDebug() << "Test";

    }

};

 

int main( int argc, char **argv )

{

    QApplication app( argc, argv );

    QGraphicsScene scene;

 

    //------------ Add Ellipse Item --------------

    MyEllipse ellipse( 1, 1, 100, 100, 0 );

    scene.addItem( &ellipse );

 

    //qDebug() << "Test"; // This works if uncommented.

 

    //--------- Create and Show View -------------

    QGraphicsView view(&scene);

    view.resize( 300, 300 );

    view.show();

 

    return app.exec();

}

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100124/83c12f4d/attachment.html 


More information about the Qt-interest-old mailing list