0

In QGraphicsView,

setDragMode(QGraphicsView::ScrollHandDrag);

This line of code did what I want: scroll contents while dragging. However, if I have implemented mouseMoveEvent or mousePressEvent for my custom needs, the setDragMode(QGraphicsView::ScrollHandDrag) magic disappears.

These built-in functions seem have some dependencies, most times they're convenient, but when you implement one of them, you break up the chain between that dependencies so that some of them gone.

I don't want to implement all these function from ground up. How to solve this dilemma?

==================Edit 12/13================

Ok, I'm trying but...

void MyView::mouseMoveEvent( QMouseEvent* event )
{

    // Notice: event->button() always return Qt::NoButton for "mouse move events"
    // so event->button() == Qt::LeftButton won't work in this case
    if(event->buttons() & Qt::LeftButton)
    {
        this->dragMoveEvent(dynamic_cast<QDragMoveEvent*>(event));
    }
    else
    {
        ...
    }


}

These code fails because QMouseEvent* cannot be cast into QDragMoveEvent*, any idea?

Alston
  • 1,166
  • 2
  • 13
  • 26
  • Have you tried calling `QGraphicsView::mouseMoveEvent()` and `QGraphicsView::mousePressEvent()` inside your `mouseMoveEvent()` and `mousePressEvent()` functions? – thuga Dec 12 '13 at 09:52
  • Thank you for suggestion. But, how do I call QGraphicsView::dragMoveEvent() inside MyView::mouseMoveEvent()? – Alston Dec 13 '13 at 07:23
  • Why would you call `QGraphicsView::dragMoveEvent` inside `MyView::mouseMoveEvent()`? – thuga Dec 13 '13 at 08:19
  • I need when mouse moving 1). without left button pressed: detect hover event so that I can fetch the pixel value of the mouse position, 2). with left button pressed: treat it as a drag event to act as setDragMode(QGraphicsView::ScrollHandDrag) – Alston Dec 14 '13 at 02:33
  • If you set drag mode to `QGraphicsView::ScrollHandDrag`, the hover events shouldn't be activated during the drag. As long as you call `QGraphicsView::mouseMoveEvent` from your own `mouseMoveEvent` function, you should be fine. – thuga Dec 16 '13 at 08:43

1 Answers1

0

@thuga is right. At the end of your subclassed functions put a reference to the original function. Like so:

void MyGraphicsView::mouseMoveEvent(QMouseEvent * e)
{
    static QPoint oldPos;
        if(!m_not_dragging)
        {
                // some logic for drawing something based on the mouse move
            // Edit added this function call.
            doMyDrag(e->pos(), oldPos);
            oldPos = e->pos();
        }
        else
        {
                QGraphicsView::mouseMoveEvent(e);
        }
}
// EDIT added the two functions below
void MyGraphicsView::dragMoveEvent(QMouseEvent * e)
{
    static QPoint oldPos;
    doMyDrag(e->pos(), oldPos);
    oldPos = e->pos();

    QGraphicsView::dragMoveEvent(e);
}

void MyGraphicsView::doMyDrag(QPoint new, QPoint old)
{
    // common drawing logic for both official drag events 
    //and pseudo drag events.
}

You could even determine m_not_dragging based on e->button() right in this function call, but this is a quick example of one way that you could have both your custom method and the default method still available.

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • Thank you for suggestion. But, how do I call QGraphicsView::dragMoveEvent() inside MyView::mouseMoveEvent()? – Alston Dec 13 '13 at 07:24
  • If a drag didn't really start, you probably should just have the core functionality of the two functions in a third function. – phyatt Dec 13 '13 at 15:34