Kirk Brodie
Kirk Brodie

Reputation: 42

Qt - drag move qgraphicitem don't work nice

My simple code base on Qt standard example 'diagram scene'. MainWindow is most simply, only load custom scene. UI is make in graphic editor. Drag and move graphic polygon work with right mouse button, left create new item. So? I must double click to item for select it. And item need double click with no release last click when I want move item. Is full dizzy.

Item code:

BasicDiagramItem::BasicDiagramItem(QGraphicsItem *parent):
    QGraphicsPolygonItem(parent)
{
    drawCustomPoly();
}

void BasicDiagramItem::drawCustomPoly()
{
    QPainterPath path;
    customPoly << QPointF(0,50) << QPointF(50,0)
              << QPointF(0,-50) << QPointF(-50,0)
              << QPointF(0,50);
    setPolygon(customPoly);
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    //setFlag(QGraphicsItem::ItemClipsToShape,true); //testowe

}

void BasicDiagramItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{

    QGraphicsPolygonItem::mouseMoveEvent(event);
}

Scene code:

DiagramScene::DiagramScene(QObject *parent):
    QGraphicsScene(parent)
{

}

void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if(mouseEvent->button() != Qt::RightButton) return;

    BasicDiagramItem *item;
    item = new BasicDiagramItem();
    //item->setBrush()
    addItem(item);
    item->setPos(mouseEvent->scenePos());
    QGraphicsScene::mousePressEvent(mouseEvent);

}

void DiagramScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    QGraphicsScene::mouseMoveEvent(mouseEvent);

}

void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    QGraphicsScene::mouseReleaseEvent(mouseEvent);
}

This code is similarity to Qt example. I just only don't use mode for create different object, don't use custom menu and I don't draw arrows.

So, what am I doing wrong? Why apps have a bug with double-click?

Upvotes: 1

Views: 593

Answers (1)

Jeremy Friesner
Jeremy Friesner

Reputation: 73041

So, what i do wrong? Why apps have a bug with double-click?

I think this is the reason:

void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
   if(mouseEvent->button() != Qt::RightButton) return;

Note that on a single-left-click, mousePressEvent() just returns without doing anything; that would explain why nothing happens when you single-left-click. (On a double-click, OTOH, mouseDoubleClickEvent() is called instead, and since you have not overridden mouseDoubleClickEvent(), the QGraphicsScene::mouseDoubleClickEvent() method is called and handles the click there.

I think what you want to do instead is this:

void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
   if(mouseEvent->button() != Qt::RightButton) 
   {
      QGraphicsScene::mousePressEvent(mouseEvent);  // call up to superclass
      return;
   }

   [...]

This way, QGraphicsScene::mousePressEvent(mouseEvent) can do the work of selecting the item that the user clicked on.

Upvotes: 1

Related Questions