[Qt-interest] Malfunctioning QGraphicsItem::ItemIsMovable?

Sherif Ghali sherif.ghali.1 at gmail.com
Tue Apr 26 17:59:38 CEST 2011


It occasionally seems that the index of QGraphicsScene is not
right. The code below demonstrates one case. If I drag the red,
green, or blue node, another node will move instead. If I replace
'-0.05' with '-0.5', dragging works normally.

// MyGraphicsScene.h
#include <QtGui>

class MyGraphicsScene : public QGraphicsScene
{
    Q_OBJECT
public:
    MyGraphicsScene()
    {
        setSceneRect(QRectF(QPointF(-2,-2), QSizeF(4,4)));
        setBackgroundBrush(Qt::black);

        insert_node( QPointF( -0.05,-0.05), Qt::red );
        insert_node( QPointF( 1,-0.05), Qt::green );
        insert_node( QPointF( -0.05,1), Qt::blue );
        insert_node( QPointF( 1,1), Qt::yellow );
    }
private:
    QGraphicsEllipseItem * insert_node(const QPointF & p, const QColor & color)
    {
        const QRectF n(QPointF(-0.1,-0.1), QSizeF(0.2,0.2));
        QGraphicsEllipseItem * node = new QGraphicsEllipseItem( n );
        node->setPen(color);
        node->setBrush(color);
        node->setFlags(QGraphicsItem::ItemIsMovable);
        node->setPos(p);
        addItem(node);
        return node;
    }
};

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

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);
    MyGraphicsScene * scene = new MyGraphicsScene;
    QGraphicsView * view = new QGraphicsView(scene);
    view->fitInView(scene->sceneRect() , Qt::KeepAspectRatio);
    view->setMinimumSize(QSize(640,480));
    view->show();
    return app.exec();
}



More information about the Qt-interest-old mailing list