Results 1 to 2 of 2

Thread: Mouse Tracking doesnt work from QCanvasView?

  1. #1
    Join Date
    Jan 2007
    Posts
    1
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Mouse Tracking doesnt work from QCanvasView?

    I'm trying to get mousetracking in a window containing a QCanvasView derived widget, but no matter from where I call setMousetracking(true), I only get called in mouseMoveEvent() if I hold down one of the mouse buttons.

    I tried calling setMousetracking() from the myview class, and from the MainWindow class, but neither work. I also tried implementing the MainWindow::mouseMoveEvent(), but it still doesnt work.

    If I just create the MainWindow (and not the myview class), and implement MainWindow::mouseMoveEvent(), calling setMouseTracking from MainWindow, then it works fine.

    Can anyone help?

    Here's my entire test app (qt 3.3.7):

    Qt Code:
    1. //main.cpp
    2.  
    3. #include <qapplication.h>
    4. #include "mainwindow.h"
    5.  
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10. pApp = &app;
    11. MainWindow mainWin(0);
    12. app.setMainWidget(&mainWin);
    13. mainWin.show();
    14. //mainWin.initCanvas();
    15. app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
    16. return app.exec();
    17. }
    18.  
    19.  
    20. ////////////////////////////////////////////////////////////////////////////////////////////
    21. #ifndef MAINWINDOW_H
    22. #define MAINWINDOW_H
    23.  
    24. #include <qobject.h>
    25. #include <qmainwindow.h>
    26. #include <qcanvas.h>
    27.  
    28. #include "myview.h"
    29.  
    30. class MainWindow : public QMainWindow
    31. {
    32. Q_OBJECT
    33.  
    34. public:
    35. MainWindow(QWidget *parent);
    36. void initCanvas();
    37. myview *pmyview;
    38. QCanvas *pcanvas;
    39.  
    40. protected:
    41. virtual void closeEvent(QCloseEvent *event);
    42. };
    43. #endif
    44. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    45. //mainwindow.cpp
    46. #include <iostream>
    47. #include <qapplication.h>
    48. #include <qcanvas.h>
    49.  
    50. #include "mainwindow.h"
    51. #include "myview.h"
    52.  
    53. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    54. {
    55. initCanvas();
    56. }
    57.  
    58. void MainWindow::initCanvas()
    59. {
    60. pmyview = new myview(this);
    61. pcanvas = new QCanvas(640,480);
    62. pmyview->setCanvas(pcanvas);
    63. setCentralWidget(pmyview);
    64. pmyview->show();
    65.  
    66. //this->pmyview->viewport()->setMouseTracking(true);
    67. //this->pmyview->setMouseTracking(true);
    68. //this->setMouseTracking(true);
    69. //setMouseTracking(true);
    70. }
    71.  
    72.  
    73. void MainWindow::closeEvent(QCloseEvent *event)
    74. {
    75. event->accept();
    76. close();
    77. }
    78. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    79.  
    80. #ifndef MYVIEW_H
    81. #define MYVIEW_H
    82.  
    83. #include <qobject.h>
    84. #include <qcanvas.h>
    85.  
    86. class myview : public QCanvasView
    87. {
    88. Q_OBJECT
    89.  
    90. public:
    91. myview(QWidget *parent);
    92.  
    93. protected:
    94. virtual void mouseMoveEvent( QMouseEvent * e);
    95.  
    96. };
    97. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    98.  
    99. //myview.cpp
    100. #include <iostream>
    101. #include "myview.h"
    102.  
    103. myview::myview(QWidget *parent) : QCanvasView(parent)
    104. {
    105. setMouseTracking(true);
    106. }
    107.  
    108. void myview::mouseMoveEvent( QMouseEvent * e)
    109. {
    110. std::cout << "moving" << std::endl;
    111. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 4th January 2007 at 01:59. Reason: missing [code] tags

  2. #2
    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: Mouse Tracking doesnt work from QCanvasView?

    You have to enable tracking for the viewport, not for the view.

  3. The following 2 users say thank you to wysota for this useful post:

    hgsw (4th January 2007), windflower (10th May 2007)

Similar Threads

  1. Replies: 7
    Last Post: 8th September 2006, 16:19
  2. Replies: 2
    Last Post: 24th July 2006, 18:36
  3. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25
  4. setCanvas blocks mouse movement on QtCanvasView
    By YuriyRusinov in forum Qt Programming
    Replies: 8
    Last Post: 20th April 2006, 07:38
  5. [QT3+XP] transparency and mouse tracking
    By incapacitant in forum Newbie
    Replies: 9
    Last Post: 17th February 2006, 18:49

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.