Results 1 to 19 of 19

Thread: Problem in MouseMoveEvent

  1. #1
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Problem in MouseMoveEvent

    I have a mainwindow inherited from QMainWindow, This contains a central widget which holds a scene and a view.

    I have overloaded mouseMoveEvent() for both central widget i am using and for mainwindow too.
    The problem is , I am not getting events in the overloaded mouseMoveEvent() in MainWindow class. How do I get the events in MainWindow's mouseMoveEvent function too ??

    I want to display the cursor position in the view in the status bar of the main window...ne suggestions ??

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem in MouseMoveEvent

    Read notes about mouse tracking in QWidget::mouseMoveEvent() docs.
    J-P Nurmi

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Problem in MouseMoveEvent

    I had read that before...
    and have set mouse tracking to true in the main window...

    i do get messages for that... but when i am at the boundary of the main window..
    when i am inside the view area, i dont get events in the main window...

    in other words, how do i set that i receive mouse evenst in the main window first and then passed on to the scene or view area ??

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem in MouseMoveEvent

    Ahh ok. It's the central widget which receives mouse move events when the mouse is over it. You can try to ignore the event so that it might get propagated to it's parent (which would be the main window). I'm not sure if it works as desired. Alternatively you can install an event filter on the central widget or deliver the position information from the central widget to the main window for example by using signals and slots..
    J-P Nurmi

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Problem in MouseMoveEvent

    Well.. I tried a bit with catching mouse movements in the main widget, but still not successfull...

    here is the class I am using for main widget..
    Qt Code:
    1. class MainWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. MainWidget(QWidget *parent=0);
    6. ~MainWidget();
    7.  
    8. private:
    9. //void setupMatrix();
    10. void populateScene();
    11.  
    12. View *view;
    13.  
    14. QList<CMacro*> macroList;
    15.  
    16. protected:
    17. void mouseMoveEvent ( QMouseEvent * event );
    18. void paintEvent(QPaintEvent *event);
    19. };
    To copy to clipboard, switch view to plain text mode 
    Here I have a scene and view.... and overloaded mouseMoveEvent function. Still when i move cursor across the view area, I dont get any events in this MainWidget::mouseMoveEvent ()

    Do i need to make my own class for scene and overload the mousevent function in that class ??

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem in MouseMoveEvent

    You can install an event filter on the view.
    J-P Nurmi

  7. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: Problem in MouseMoveEvent

    well.. quite late to solve this problem i was away fr some days...

    back to the problem... i tried deriving classess and handling events through mousemoveevents but in vain.
    I also installed eventfilter in mainwindow(derived frm QMainWindow) , but i dont receive mousemoveevents in that :-(

    Next I tried sending event from view to parent (CViewFrame derived from QFrame). This worked, and I was able to track cursor position in Mainwindow's status bar. But problem occured in moving graphic items in the view.

    Is there not any simple way to know the cursor position in the QMainWindow ??
    Even if i install event filter, i dont receive QEvent::MouseMove or QEvent::HoverEnter. What event is passed on to QMainWindow when mouse moves ??? and how do i capture it in event filter ??

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Problem in MouseMoveEvent

    aaahhh... shud have waited a while to post above..

    finally got the solution though not much efficient..

    i traced the cursor in paintevent of Qmainwindow...and set its position in statusbar !!
    though it slows down moving of items in the view, it does solve the problem to some extent.... simple solution nah

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem in MouseMoveEvent

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. public:
    6. MainWindow(QWidget* parent = 0) : QMainWindow(parent)
    7. {
    8. QGraphicsItem* item = scene->addText("Item");
    9. item->setFlag(QGraphicsItem::ItemIsMovable);
    10.  
    11. view = new QGraphicsView(scene);
    12. view->viewport()->installEventFilter(this);
    13. // this is not actually even needed, QGraphicsView
    14. // already sets the mouse tracking on on it's viewport
    15. view->viewport()->setMouseTracking(true);
    16. setCentralWidget(view);
    17. }
    18.  
    19. bool eventFilter(QObject* object, QEvent* event)
    20. {
    21. if (event->type() == QEvent::MouseMove)
    22. {
    23. QMouseEvent* mouse = static_cast<QMouseEvent*>(event);
    24. QPointF pos = view->mapToScene(mouse->pos());
    25. statusBar()->showMessage(QString("(%1,%2)").arg(pos.x()).arg(pos.y()));
    26. }
    27. return false;
    28. }
    29.  
    30. private:
    31. };
    32.  
    33. int main(int argc, char* argv[])
    34. {
    35. QApplication app(argc, argv);
    36. MainWindow w;
    37. w.show();
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 8th November 2006 at 08:47.
    J-P Nurmi

  10. The following user says thank you to jpn for this useful post:

    aamer4yu (8th November 2006)

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem in MouseMoveEvent

    Subclassing the view and ignoring its mouse events (one has to specifically call ignore() on the event object in mouse event handlers) should work as well.

  12. The following user says thank you to wysota for this useful post:

    aamer4yu (8th November 2006)

  13. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: Problem in MouseMoveEvent

    @jpn

    Thanks, ur solution is working fine...also the movement of items is smooth

    I was also pretty much using the same code...
    Qt Code:
    1. class CMainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. CMainWindow(QWidget *parent=0);
    6. ~CMainWindow();
    7.  
    8. private:
    9. //void setupMatrix();
    10. void populateScene();
    11.  
    12. CViewFrame *viewFrame; // CViewFrame derived from QFrame
    13.  
    14. //QGraphicsScene *scene;
    15. CScene *scene;
    16. QList<CMacro*> macroList;
    17.  
    18. protected:
    19. bool eventFilter(QObject *obj, QEvent *event);
    20. void mouseMoveEvent ( QMouseEvent * event );
    21. void paintEvent(QPaintEvent *event);
    22. };
    23.  
    24.  
    25. CMainWindow::CMainWindow(QWidget *parent)
    26. : QMainWindow(parent)
    27. {
    28. setMouseTracking(true);
    29.  
    30. populateScene();
    31.  
    32.  
    33. viewFrame = new CViewFrame("demo",this);
    34. viewFrame->getView()->setScene(scene);
    35. viewFrame->getView()->setMouseTracking(true);
    36.  
    37. viewFrame->getView()->setSceneRect(-100,-100,1700,2500);
    38. //setContentsMargins(200,200,600,600);
    39.  
    40. //scene->setBackgroundBrush(Qt::blue);
    41.  
    42.  
    43. setCentralWidget(viewFrame);
    44. viewFrame->getView()->installEventFilter(this);
    45. //viewFrame->getView()->installEventFilter(this);
    46. ....
    47. ...
    48. ...
    To copy to clipboard, switch view to plain text mode 
    The only mistake was i was installing filter on view... rather than viewport
    The events are now captured in QEvent::MouseMove of the filter...

    So what does actually viewport act like ? why doesnt installing filter on view capture the events ??



    @wysota

    Ur solution too is working and short & elegant
    Can u tell me more on event delegation ?? I saw thru the call stack and came to conclude that...that Qt uses QApplicationPrivate::notify_helper() to properly distrubute the events, and hence its View in the window that receives the events first.

    but what will ignore do and how will the events propogate further ?? can u throw more light ?


    Thanks a lot both of u guys...just wondering what took u so long to answer
    neways it helped me in exploring & learning myself.

    Thx again

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem in MouseMoveEvent

    Quote Originally Posted by aamer4yu View Post
    Ur solution too is working and short & elegant
    It wasn't my solution. Jpn already told you about it in his second post in this thread.

    Can u tell me more on event delegation ?? I saw thru the call stack and came to conclude that...that Qt uses QApplicationPrivate::notify_helper() to properly distrubute the events, and hence its View in the window that receives the events first.
    I can, but I doubt I can tell you more than you can read from the docs. In general the mechanism is simmilar to the one present in (for example) MFC - the events are propagated from class to class until one of them marks them as handled. In case of Qt the order is child widget -> its parent -> its parent -> ... -> application object.

    but what will ignore do and how will the events propogate further ?? can u throw more light ?
    I think I already answered that

    Thanks a lot both of u guys...just wondering what took u so long to answer
    Your question was answered at the very beginning, you just ignored that answer (instead of ignoring the event).

  15. #13
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in MouseMoveEvent

    Your question was answered at the very beginning, you just ignored that answer (instead of ignoring the event).
    No, I didnt ignore that... as i said i installed filter on view...and tried all sort of things
    i didt knew i had to install them on viewport...

    earlier answers were like showing which tool to use... but didnt tell where to use
    or may b i was ameatuer in Qt that i didnt understand what they meant

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem in MouseMoveEvent

    Following the "ignore the event" link in JPN's post would have lead you to the right answer - event propagation.

  17. #15
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in MouseMoveEvent

    well i said, I had tried that too... but might be making some mistake somewhere...

    neways dont fight... I take my thanks back from u ...
    Jpn... double thnks to u

    is that fine now wysota :P

  18. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem in MouseMoveEvent

    Quote Originally Posted by aamer4yu View Post
    is that fine now wysota :P
    It's your call.

  19. #17
    Join Date
    Oct 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Post Re: Problem in MouseMoveEvent

    hii....wysota .i am also having same problem with mouse events .now i am getting mouse events fine in QGraphicsscene .but i want to add a push button in form for mouse events to draw rectangle and it is moveble .so how can i proceed ..? can any one have idea...?

  20. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem in MouseMoveEvent

    Quote Originally Posted by ranjithreddykommareddy View Post
    hii....wysota .i am also having same problem with mouse events .now i am getting mouse events fine in QGraphicsscene .but i want to add a push button in form for mouse events to draw rectangle and it is moveble .so how can i proceed ..? can any one have idea...?
    Show your (relevant) code, please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. #19
    Join Date
    Oct 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Problem in MouseMoveEvent

    thanks for reply
    bool Widget:: eventFilter(QObject* object, QEvent* event)
    {


    if (event->type() == QEvent::MouseButtonPress)
    {
    QMouseEvent* mouse = static_cast<QMouseEvent*>(event);
    if (ui->graphicsView->geometry().contains(mouse->pos()))
    {
    originpos = ui->graphicsView->mapToScene(mouse->pos());
    // qDebug() << mouse->pos() << originpos ;
    mousepress = true;
    }
    //statusBar()->showMessage(QString("(%1,%2)").arg(pos.x()).arg(p os.y()));


    }
    if(mousepress)
    {
    if (event->type() == QEvent::MouseMove)

    {


    QMouseEvent* mouse = static_cast<QMouseEvent*>(event);

    if (ui->graphicsView->geometry().contains(mouse->pos()))
    {
    currentpos = ui->graphicsView->mapToScene(mouse->pos());
    //qDebug() << mouse->pos() ;



    if(!itemToDraw)
    {
    itemToDraw = new QGraphicsRectItem;

    ui->graphicsView->scene()->addItem(itemToDraw);
    itemToDraw->setPen(QPen(Qt::black, 1, Qt::SolidLine));


    //itemToDraw->setPos(pos);
    }
    itemToDraw->setRect(originpos.x(),originpos.y(),
    currentpos.x()- originpos.x(),
    currentpos.y() - originpos.y());



    }
    //itemToDraw->setFlag(QGraphicsRectItem::ItemIsMovable);
    }


    }



    if(mousepress)
    {
    if(event->type() == QEvent::MouseButtonRelease )
    {
    QMouseEvent* mouse = static_cast<QMouseEvent*>(event);
    QPointF pos = ui->graphicsView->mapToScene(mouse->pos());
    // qDebug() << pos ;
    mousepress = false;

    }
    }



    }
    Last edited by ranjithreddykommareddy; 3rd December 2015 at 11:28.

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.