Results 1 to 20 of 28

Thread: Mouse Press/Release Events

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 07:50.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    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,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 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,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 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,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 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,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 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,
    _

Similar Threads

  1. Replies: 3
    Last Post: 8th October 2011, 09:46
  2. Replies: 6
    Last Post: 27th January 2011, 17: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, 20:53
  4. Problem in Mouse Press Events & Vectors
    By dheeraj in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2008, 18:08
  5. Replies: 2
    Last Post: 2nd April 2008, 14: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.