Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: Mouse Press/Release Events

  1. #1
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Mouse Press/Release Events

    Dear All,

    Im getting mouseevent at when i click the mouse on the GraphicsScene. Im getting the position/co-ordinate also, where as release event im getting the co-ordinate/positions. please Suggest me
    Qt Code:
    1. #ifndef APP_H
    2. #define APP_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGraphicsScene>
    6. #include <QPoint>
    7. #include <QMouseEvent>
    8.  
    9. protected:
    10. void changeEvent(QEvent *e)
    11. void mousePressEvent (QMouseEvent *e)
    12. void mouseReleaseEvent (QMouseEvent *e)
    13.  
    14. private:
    15. int x,x1,y,y1;
    16. QPoint *point;
    17.  
    18. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "app.h"
    2. #include "ui_app.h"
    3.  
    4. APP::APP(QWidget *parent):
    5.  
    6. QMainWindow(parent),
    7. ui(new Ui::APP)
    8. {
    9. ui->setupUi (this);
    10. }
    11.  
    12. void APP::mousePressEvent(QMouseEvent *e)
    13. {
    14. QPoint point=e->pos();
    15. x=point.x();
    16. qDebug()<<x;
    17.  
    18. y=point.y();
    19. qDebug()<<y
    20.  
    21. }
    22.  
    23. void APP::mouseReleaseEvent(QMouseEvent *e)
    24. {
    25. QPoint point=e->pos();
    26. x1=point.x();
    27. qDebug()<<x1;
    28.  
    29. y1=point.y();
    30. qDebug()<<y1;
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 


    Im getting x,y position data of Graphic scene when i click on scene, but not x1, y1 positions when i release mouse. By getting all co-ordinates i can draw line.
    Last edited by Vivek1982; 22nd July 2014 at 08:50.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Mouse Press/Release Events

    Call base class implementations of these event handlers after your class-specific code
    Qt Code:
    1. void APP::mousePressEvent(QMouseEvent *e)
    2. {
    3. //... my code
    4. QMainWindow::mousePressEvent(e);
    5. }
    6.  
    7. void APP::mouseReleaseEvent(QMouseEvent *e)
    8. {
    9. // ... my code
    10. QMainWindow::mouseReleaseEvent(e);
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mouse Press/Release Events

    Hi..

    I tried by calling base class, but still same result. Im getting only MousePressEvent Co-ordinates, but not the Co-ordinates where i released the Mouse. When I click mouse after dragging I get mouse click event co-ordinates.

  4. #4
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mouse Press/Release Events

    Thanks alot for your replies. I got both the co-orodinates of Mouse on click and release events. Actually in my widget as QMainWindow. I have QGraphicsScene and QGraphicsView placed in my QMainWindow. scene has been created inside MainWindow.

    Im getting mouse press and release event or positions on QMainWindow, whereas when i click on my GraphicScene area, im not getting press/release events on Scene. outside the graphicscene area im getting it..

    How can i get the co-ordinates on graphicScene area.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Mouse Press/Release Events

    If you want the click events when the mouse is over the QGraphicsView widget then you can either:
    • Reimplement and use the mouseClickEvent() and mouseReleaseEvent() of the QGraphicsView object.
    • Install an event filter on the QGraphicsView object.

  6. #6
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mouse Press/Release Events

    Ya. I tried to implement both Mouse click and release events still the same release is not working on graphicsview.

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGraphicsView>
    6. #include <QGraphicsScene>
    7. #include <QGraphicsSceneMouseEvent>
    8. #include <QPointF>
    9. #include <QMouseEvent>
    10. #include <QDebug>
    11. namespace Ui {
    12. class MainWindow;
    13. }
    14.  
    15. class MainWindow : public QMainWindow {
    16. Q_OBJECT
    17. public:
    18. MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. protected:
    22. void changeEvent(QEvent *e);
    23. protected:
    24. void mousePressEvent(QMouseEvent * e);
    25. void mouseReleaseEvent(QMouseEvent * e);
    26.  
    27. private:
    28. Ui::MainWindow *ui;
    29. QPointF *point,*point1;
    30. float x,y,x1,y1;
    31. };
    32.  
    33. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. scene=new QGraphicsScene(0,0,740,400);
    10. ui->graphicsView->setScene(scene);
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::changeEvent(QEvent *e)
    19. {
    20. QMainWindow::changeEvent(e);
    21. switch (e->type()) {
    22. case QEvent::LanguageChange:
    23. ui->retranslateUi(this);
    24. break;
    25. default:
    26. break;
    27. }
    28.  
    29. }
    30.  
    31. void MainWindow::mousePressEvent(QMouseEvent *e)
    32. {
    33.  
    34. if(e->MouseButtonPress)
    35. {
    36. qDebug("clicked");
    37. QPointF point= ui->graphicsView->mapFromScene(e->posF());
    38. x=point.x();
    39. qDebug()<<x;
    40. y= point.y();
    41. qDebug()<<y;
    42. qDebug()<<point;
    43.  
    44. }
    45. }
    46.  
    47.  
    48.  
    49. void MainWindow::mouseReleaseEvent(QMouseEvent *e)
    50. {
    51.  
    52. if(e->MouseButtonRelease)
    53. {
    54. qDebug("released");
    55. QPointF point1= ui->graphicsView->mapFromScene(e->posF());
    56. x1=point1.x();
    57. qDebug()<<x1;
    58. y1= point1.y();
    59. qDebug()<<y1;
    60. qDebug()<<point1;
    61. }
    62.  
    63. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mouse Press/Release Events

    Quote Originally Posted by Vivek1982 View Post
    Ya. I tried to implement both Mouse click and release events still the same release is not working on graphicsview.
    Instead of implementing the main window's event handler, you could try what ChrisW67 suggested.

    Cheers,
    _

  8. #8
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mouse Press/Release Events

    I tried with event filter. i didnt get both press and release positions on GraphicsScene widget.I saw examples in
    http://stackoverflow.com/questions/7...s-not-register

    but still working on, how ever it is mouse press is okay.. but release pos is not coming.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mouse Press/Release Events

    Tried you implementing the graphicsview's eventhandlers?

    Cheers,
    _

  10. #10
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mouse Press/Release Events

    Yes. reimplemented by sub class QGraphicsView and also calling event handlers of QGraphicsView. Im getting error like :-1: error: collect2: ld returned 1 exit status

    Qt Code:
    1. #include <QDebug>
    2. namespace Ui {
    3. class MainWindow;
    4. }
    5.  
    6. class MainWindow : public QMainWindow {
    7. Q_OBJECT
    8. public:
    9. MainWindow(QWidget *parent = 0);
    10. ~MainWindow();
    11.  
    12. protected:
    13. void changeEvent(QEvent *e);
    14.  
    15.  
    16. private:
    17. Ui::MainWindow *ui;
    18. QPointF *point,*point1;
    19. float x,y,x1,y1;
    20. };
    21. class MyGraphicsView : public QGraphicsView
    22. {
    23. Q_OBJECT
    24. public:
    25. MyGraphicsView(QWidget *parent=NULL);
    26. ~MyGraphicsView();
    27.  
    28. protected:
    29. void mousePressEvent(QGraphicsSceneMouseEvent * event);
    30. void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
    31.  
    32. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MyGraphicsView::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3.  
    4. if(event->MouseButtonPress)
    5. {
    6. qDebug("clicked");
    7. QPointF point= event->pos();//ui->graphicsView->mapFromScene(event->pos());
    8. //x=point.x();
    9. //qDebug()<<x;
    10. //y= point.y();
    11. //qDebug()<<y;
    12. qDebug()<<point;
    13.  
    14. }
    15. }
    16.  
    17.  
    18.  
    19. //void MainWindow::mouseReleaseEvent(QMouseEvent *event)
    20. void MyGraphicsView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    21. {
    22.  
    23. if(event->MouseButtonRelease)
    24. {
    25. qDebug("released");
    26. QPointF point1=event->pos();//ui->graphicsView->mapFromScene(event->pos());
    27. //x1=point1.x();
    28. //qDebug()<<x1;
    29. //y1= point1.y();
    30. //qDebug()<<y1;
    31. qDebug()<<point1;
    32. }
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mouse Press/Release Events

    Quote Originally Posted by Vivek1982 View Post
    Yes. reimplemented by sub class QGraphicsView and also calling event handlers of QGraphicsView. Im getting error like :-1: error: collect2: ld returned 1 exit status
    You'll have to post the full error message, really.

    Also it would help a lot if you implemented the event handler methods of QGraphicsView, not those of QGraphicsScene.
    Unless your goal is to have methods that are never called, in which case that is of course fine.

    Cheers,
    _

  12. #12
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mouse Press/Release Events

    Ya.. the same is implemented. event handlers are there in QGraphicView only. but to check for the positions im using QGraphicSceneMousePress events...

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mouse Press/Release Events

    Quote Originally Posted by Vivek1982 View Post
    Ya.. the same is implemented. event handlers are there in QGraphicView only.
    Your code above did not. There is are no such two methods in QGraphicsView.

    Cheers,
    _

  14. #14
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mouse Press/Release Events

    ok. let me tell the approach how to get mousevents on QGraphicView. Initially i have mainwindow widget along with the Ui file. In the Ui file i have dragged a Graphic view to mainwindow ui. when my mouse events are working fine on mainwindow widget. or i need to create separate mouseevent.cpp and mouseevent.h and call the positions in mainwindow as seen in scriblearea example. suggest how it can be done in other ways.
    Or help with steps/procedure to how to set event filter to get mouse events on graphicview on mainwindow.
    Last edited by Vivek1982; 8th August 2014 at 08:33.

  15. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Mouse Press/Release Events

    Quote Originally Posted by Vivek1982 View Post
    suggest how it can be done in other ways.
    You could create a subclass if QGraphicsView and implement the mouse event handler methods. Then promote the GraphicsView in designer to that class.

    Cheers,
    _

  16. #16
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mouse Press/Release Events

    ok. will create my Ui form in mainwindow form. But declaration of GraphicsView to be done in sub class mouseevent.cpp and mouseevent.h, which is not having the Ui form. Im confused by seeing Scribblearea example. Here positions are collected from scribblearea.cpp and same is brought on mainwindow of class. Let me give a try. or i will post the code. based on comments i will try to do it. Thats wat i can do now

  17. #17
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mouse Press/Release Events

    Dear All,

    Finally I got mouse press/release event on Graphicscene by eventfilter. Im getting the co-ordinates by mouse events. For the same positions to draw lines I have made paint event,but calling paint event from switch (event filter) is not possible. Any methods to call the paint event.

    Thanks in advance

  18. #18
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Mouse Press/Release Events

    calling paint event from switch (event filter) is not possible
    Store the mouse coordinates and use them later in paintEvent.

  19. #19
    Join Date
    Sep 2013
    Posts
    107
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mouse Press/Release Events

    ya.. Im able to get mouse positions on QpointF .. but in event filter points are stored .. by releasing the mouse button it has to draw line.. in header file i have void PaintEvent (QPaintEvent *event ).
    but it has to be called after release button is left...
    In header.cpp i have void MainWindow::PaintEvent then following by painter pen then draw line.. the positions are from event filter...

  20. #20
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Mouse Press/Release Events

    by releasing the mouse button it has to draw line
    Call "update()" after storing the coordinates and releasing the mouse button.

Similar Threads

  1. Replies: 3
    Last Post: 8th October 2011, 10:46
  2. Replies: 6
    Last Post: 27th January 2011, 18:06
  3. How catch key press and key release for entire application
    By hubbobubbo in forum Qt Programming
    Replies: 4
    Last Post: 1st June 2010, 21:53
  4. Problem in Mouse Press Events & Vectors
    By dheeraj in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2008, 19:08
  5. Replies: 2
    Last Post: 2nd April 2008, 15:19

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.