Results 1 to 2 of 2

Thread: Problemas with mouseMoveEvent - Wont Catch Any Movement

  1. #1
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1

    Default Re: Problemas with mouseMoveEvent - Wont Catch Any Movement

    Hey,

    I'm having trouble setting up mouseMoveEvent. I setted up a mousePressEvent and it works just fine, but I wont get any response from the
    mouseMoveEvent.

    I need to catch the mouse position when its moving. Its not implemented yet, but once I grab hold of the mouse movements I will send the positions that are changing to a function to process these movements.

    This is the code that I have:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGraphicsScene>
    6. #include <QGraphicsLineItem>
    7. #include <QGraphicsSceneMouseEvent>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. virtual void mousePressEvent(QMouseEvent *qme1);
    20. virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mme2);
    21. void createLine();
    22. qreal x1,x2,y1,y2;
    23. int i;
    24.  
    25.  
    26. ~MainWindow();
    27.  
    28. public slots:
    29. void criarLinha();
    30.  
    31.  
    32.  
    33. private:
    34. Ui::MainWindow *ui;
    35. };
    36.  
    37. #endif // MAINWINDOW_H
    38.  
    39. -------//------
    40.  
    41. #include "mainwindow.h"
    42. #include "ui_mainwindow.h"
    43. #include <QGraphicsScene>
    44. #include <QGraphicsLineItem>
    45. #include <QEvent>
    46. #include <QDropEvent>
    47. #include <QSignalMapper>
    48. #include <QMouseEvent>
    49. #include <QtDebug>
    50.  
    51.  
    52. MainWindow::MainWindow(QWidget *parent) :
    53. QMainWindow(parent),
    54. ui(new Ui::MainWindow)
    55. {
    56. ui->setupUi(this);
    57. MainWindow::setCentralWidget(ui->graphicsView);
    58. QObject::connect(ui->action_Line,SIGNAL(triggered()),this,SLOT(criarLinha()));
    59. ui->graphicsView->setMouseTracking(true);
    60.  
    61.  
    62. i = 0;
    63. x1 = 0;
    64. y1 = 0;
    65. x2 = 0;
    66. y2 = 0;
    67.  
    68. scene = new QGraphicsScene;
    69. ui->graphicsView->setScene(scene);
    70. scene->installEventFilter(this);
    71.  
    72.  
    73.  
    74. }
    75.  
    76. MainWindow::~MainWindow()
    77. {
    78. delete ui;
    79. }
    80.  
    81. // Pulbic Functions
    82. void MainWindow::createLine()
    83. {
    84. QGraphicsLineItem *linha = new QGraphicsLineItem(x1,y1,x2,y2);
    85. scene->addItem(linha);
    86. }
    87.  
    88. void MainWindow::mousePressEvent(QMouseEvent *qme1)
    89. {
    90. QMouseEvent *event = static_cast<QMouseEvent *>(qme1);
    91.  
    92.  
    93.  
    94. if((event->button() == Qt::LeftButton))
    95. {
    96. if(i == 0)
    97. {
    98. x1 = dynamic_cast<QMouseEvent*>(event)->x();
    99. y1 = dynamic_cast<QMouseEvent*>(event)->y();
    100. qDebug() << "x1 é igual a:" << x1;
    101. qDebug() << "y1 é igual a:" << y1;
    102. i = 1;
    103.  
    104.  
    105. }
    106. else
    107. {
    108. x2 = dynamic_cast<QMouseEvent*>(event)->x();
    109. y2 = dynamic_cast<QMouseEvent*>(event)->y();
    110. qDebug() << "x2 é igual a:" << x2;
    111. qDebug() << "y2 é igual a:" << y2;
    112. i = 0;
    113. }
    114.  
    115. }
    116. }
    117.  
    118. void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEvent *mme)
    119. {
    120.  
    121. qDebug() << "PQP estou mexendo!!";
    122.  
    123. }
    124.  
    125.  
    126.  
    127. //Public Slots
    128. void MainWindow::criarLinha()
    129. {
    130. createLine();
    131. }
    To copy to clipboard, switch view to plain text mode 

    any help will be appreciated.

    tnx,


    Added after 54 minutes:


    I made it work.

    I had to create a class derived from QGraphicView and do another implementation of mouseMoveEvent under it. Any1 can explain why this is necessary?

    Why couldnt I just have done it in QMainWindow since it is its central ?
    Last edited by 21altf; 29th October 2011 at 19:07.

  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: Problemas with mouseMoveEvent - Wont Catch Any Movement

    Why couldnt I just have done it in QMainWindow since it is its central ?
    Have you tried to setMouseTracking(true) ?
    From the docs:
    If mouse tracking is switched off, mouse move events only occur if a mouse button is pressed while the mouse is being moved. If mouse tracking is switched on, mouse move events occur even if no mouse button is pressed.

Similar Threads

  1. qwtplot wont resize
    By marc2050 in forum Qwt
    Replies: 1
    Last Post: 31st July 2011, 13:08
  2. Program wont start - Wont even enter main()
    By ComServant in forum General Programming
    Replies: 5
    Last Post: 8th January 2011, 03:44
  3. Qt Opensource 4.5.2 wont compile
    By MarkoSan in forum Installation and Deployment
    Replies: 8
    Last Post: 26th November 2009, 06:56
  4. OSX App wont run on other machines
    By December in forum Qt Programming
    Replies: 3
    Last Post: 13th April 2007, 02:23
  5. freebsd 6 -> kdevelop wont uic
    By soul_rebel in forum KDE Forum
    Replies: 0
    Last Post: 5th January 2006, 22:00

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.