PDA

View Full Version : QGrahicsItem: problem with ItemIsMovable (dragging rectangle bigger than item itself)



penpen
24th March 2011, 18:38
Hello,

I have a problem with a QGraphicsItem in a QGraphicsScene. What I would like is to be able to move the item with mouse. But the problem is that in my case, the dragging rectangle is bigger than the item itself.

Here is the code that I use:


class Test: public QGraphicsView
{
public:
Test();

private:
virtual void resizeEvent (QResizeEvent * event);
QGraphicsScene* m_pScene;
};

Test::Test()
{
m_pScene = new QGraphicsScene();
setScene(m_pScene);

m_pScene->setSceneRect(0, 0, 100, 100);

for (int i = 0 ; i < 10 ; i++)
{
QGraphicsRectItem * pItem = new QGraphicsRectItem();
pItem->setFlag(QGraphicsItem::ItemIsMovable);
pItem->setBrush(QBrush(QColor(190, 100, 100)));
pItem->setRect(QRectF(10*i, 10, 5, 80.f));
pItem->setCursor(Qt::SizeAllCursor);
m_pScene->addItem(pItem);
}


setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;

resize(600, 200);
fitInView(scene()->sceneRect());
show();
}

void Test::resizeEvent(QResizeEvent * event)
{
QGraphicsView::resizeEvent(event);
fitInView(scene()->sceneRect());
}


So when I run my program I have this window, and I can drag items. All seems ok.
6143

But if I look closer the dragging zone is bigger than the item itself. (see the blue rectangle on following screenshot) The blue rectangle means that If I move the mouse in this rectangle, the cursor changes, and I can drag the item.
6144

I have the feeling that the problem is related to the "fitInView(scene()->sceneRect());" line. If I remove the fitInView but adds a scale(5,1) for example, there is the same problem.

Do you have an idea of what the problem could be? Am I doing something wring with coordinates?

high_flyer
1st April 2011, 15:29
what is the bounding rect that the item returns?
Is it the same as when you set it?