[SOLVED] mousePressEvent called only from certain areas in scene



  • EDIT: Almost solved, see first reply!

    I have an application which draws lines based on different data from cars. I want my application to be able to select the lines drawn, and then make the corresponding item selected in a list on the left as well. The problem is that the mousePressEvent is only called when I press the mousebutton in the rightmost quarter of the scene. When it is called the curveSelected() function works as well, but I can't figure out why I can't invoke the mousePressEvent from the other areas on the scene.

    First of all I have a mousePressEvent.
    @
    void DrawingScene::mousePressEvent ( QGraphicsSceneMouseEvent * event ){
    event->ignore();
    bool leftbutton = (event->button() == Qt::LeftButton);
    if(leftbutton)
    {
    qDebug() << "leftbutton";
    emit leftButtonPress(event->scenePos());
    }
    QGraphicsScene::mousePressEvent(event);
    }
    @
    Later connected:
    @
    connect(d_scene, SIGNAL(leftButtonPress(QPointF)), this, SLOT(curveSelected(QPointF)));
    @

    leftButtonPress is the signal emitted. Then I have the function which selects the item in the list. This method seems to work just fine. The problem exists without this function as well.

    @
    void CurveDrawer::curveSelected(QPointF pos){
    QMapIterator<QPair<unitID, QString>, carData*> it(dataMap);
    while(it.hasNext()){
    it.next();
    QPainterPath curPath = it.value()->pathItem->path();
    if(curPath.contains(pos)){
    for (int i = 0; i < list->count(); ++i) {
    QListWidgetItem* curItem = list->item(i);
    if(curItem == it.value()->listItem){
    qDebug() << "curveSelected";
    curItem->setSelected(true);
    }
    }
    }
    }
    }
    @

    Anyone experienced something similar, or may see some obvious mistakes in my code?

    How can i achieve that the mousePressEvent is called every time I click inside the scene? This is basically what I want it to do. Now it is only called when I click in a certain area.

    Noticed that this guy had some similar problem: http://www.qtcentre.org/threads/37031-GraphicsView-mousePressEvent-problem--

    EDIT: I tried to implement it with void DrawGraphicsView;;mousePressEvent(QMouseEvent *event) now, and the same problem existed there. The event just got invoked from certain areas in the scene.

    EDIT2: The strange thing for me is that when a certain place in the scene is in the left of the viewport it is not possible to invoke the mousepressEvent, but when I scroll the same place to the right in the viewport, then it is suddenly possible to invoke the mousepressEvent. Does this make the problem clearer?



  • This was my first post here so just tell me if my approach to describe the problem was wrong in some way :)

    Anyway, I managed to solve the problem in some way. In my my scene, I have to sliders, one vertical and one horizontal. They are placed in the top left corner. If you draw an "invisible" line from the end of both sliders, they form a rectangle explicitly not shown. This area was the area I couldn't receive mousePressEvents from. My quickfix was to make the sliders smaller and then have almost the whole scene as a place where I could get events from.

    But how could I still have these sliders big and still receive events from the same area (preferably the whole area)?

    The sliders are placed in a gridlayout to make them appear in the topleft corner and I have then placed the layout in a widget with the graphicsview as its parent.



  • [quote author="IBalic" date="1342101652"]The sliders are placed in a gridlayout to make them appear in the topleft corner and I have then placed the layout in a widget with the graphicsview as its parent.[/quote]

    I don't quite understand how you did that, but that sounds like that widget is your problem. Could it be that the widget that holds the sliders partly covers your graphicsview, but is invisible? Then it would of course get the click events; and that would explain why the area is smaller when the sliders are smaller.

    I'm not sure where you want to put these sliders, but "in a widget with the graphicsview as its parent" does not sound like a good solution. I think the only children of the graphicsview should be the viewport, the scrollbars, and maybe the corner widget.



  • Yeah, you are right. I placed the widget outside the graphicsview, and that solved the problem. Thank you!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.