Results 1 to 19 of 19

Thread: Problem in MouseMoveEvent

Hybrid View

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

    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 09:47.
    J-P Nurmi

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

    aamer4yu (8th November 2006)

  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.

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

    aamer4yu (8th November 2006)

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

    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

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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).

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

    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

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.

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

    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

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.

  11. #9
    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...?

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


  13. #11
    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 12:28.

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 13:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 13:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 15:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 22:36
  5. Replies: 16
    Last Post: 7th March 2006, 16: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
  •  
Qt is a trademark of The Qt Company.