Results 1 to 14 of 14

Thread: setMouseTracking() doesn't work.

  1. #1
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default setMouseTracking() doesn't work.

    Hi guys:
    I have a problem about the setMouseTracking(). I want to receive the mouseMoveEvent even if no mouse button pressed. As the Qt Document says, I set the mouseTracking property TRUE by setMouseTracking(TRUE),but itdoesn't work.
    My app has a MainWindow and a QWidgetStack as the MainWindow's main widget. The WidgetStack contains two widgets. One is QTextEdit, another is QCanvasView. Since I want to deal with the mouseMoveEvent within the QCanvasView, I subclass my own canvasview from QCanvasView. Then in the constructor of my own canvasview I set
    the mouseTracking property TRUE. But I'm confused that the contentsMouseMoveEvent() is only called when a mousebutton pressed and moved just as mouseTracking is FALSE.

    bellow is my code:
    mainwindow.h
    Qt Code:
    1. #include <qdockwindow.h>
    2. #include <qwidgetstack.h>
    3. #include <qtoolbutton.h>
    4. #include <qtextedit.h>
    5. #include <qcanvas.h>
    6. #include "canvasview.h"
    7. class MainWindow: public QMainWindow
    8. {
    9. Q_OBJECT
    10. public:
    11. MainWindow(QWidget *parent=0,const char* name = 0);
    12. private slots:
    13. void switchToolBarMode();
    14. void switchCentralWidgetMode(int);
    15. private:
    16. void init();
    17. QWidgetStack* centralWidgetStack;
    18. QDockWindow* toolBarDockWindow;
    19. QWidgetStack* toolBarStack;
    20. QToolButton* graphicButton;
    21. QToolButton* quitButton;
    22. QTextEdit *textEdit;
    23. CanvasView* canvasView;
    24. QCanvas* canvas;
    25. bool toolBarMode;
    26. };
    27. #endif
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "canvasview.h"
    3. #include <qmainwindow.h>
    4. #include <qapplication.h>
    5. #include <qlayout.h>
    6.  
    7. MainWindow::MainWindow(QWidget* parent, const char* name)
    8. :QMainWindow(parent,name)
    9. {
    10. int screenW;
    11. int screenH;
    12. screenW = QApplication::desktop()->width();
    13. screenH = QApplication::desktop()->height();
    14. this->resize(screenW,screenH);
    15. toolBarMode = TRUE;
    16. init();
    17. }
    18.  
    19. void MainWindow::init()
    20. {
    21. toolBarDockWindow = new QDockWindow(QDockWindow::InDock,this,"toolbardockwindow",0);
    22. toolBarDockWindow->setOrientation(Qt::Horizontal);
    23. toolBarDockWindow->setHorizontallyStretchable(TRUE);
    24. toolBarDockWindow->setVerticallyStretchable(TRUE);
    25. toolBarDockWindow->setMovingEnabled(FALSE);
    26. this->moveDockWindow(toolBarDockWindow,Qt::DockBottom);
    27.  
    28. toolBarStack = new QWidgetStack(toolBarDockWindow);
    29. toolBarDockWindow->setWidget(toolBarStack);
    30.  
    31. QWidget* editToolBar = new QWidget(toolBarStack);
    32. QHBoxLayout* editToolBarLayout = new QHBoxLayout(editToolBar,6,6);
    33. QWidget* graphicToolBar = new QWidget(toolBarStack);
    34. QHBoxLayout* graphicToolBarLayout = new QHBoxLayout(graphicToolBar,6,6);
    35. toolBarStack->addWidget(editToolBar,0);
    36. toolBarStack->addWidget(graphicToolBar,1);
    37.  
    38. graphicButton = new QToolButton(editToolBar,"graphicutton");
    39. graphicButton->setUsesTextLabel(TRUE);
    40. graphicButton->setTextLabel("Graphic",FALSE);
    41. graphicButton->setFont(QFont("Times",20,QFont::Bold));
    42. editToolBarLayout->addWidget(graphicButton);
    43.  
    44. QSpacerItem* editToolBarSpacer = new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Minimum);
    45. editToolBarLayout->addItem(editToolBarSpacer);
    46. quitButton = new QToolButton(graphicToolBar,"quitbutton");
    47. quitButton->setUsesTextLabel(TRUE);
    48. quitButton->setTextLabel("Return",FALSE);
    49. quitButton->setFont(QFont("Times",20,QFont::Bold));
    50. graphicToolBarLayout->addWidget(quitButton);
    51.  
    52. QSpacerItem* graphicToolBarSpacer = new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Minimum);
    53. graphicToolBarLayout->addItem(graphicToolBarSpacer);
    54.  
    55. toolBarStack->raiseWidget(0);
    56.  
    57. connect(graphicButton,SIGNAL(clicked()),this,SLOT(switchToolBarMode()));
    58. connect(quitButton,SIGNAL(clicked()),this,SLOT(switchToolBarMode()));
    59.  
    60. //construct the QWidgetStack as the MainWindow's main widget.
    61. centralWidgetStack = new QWidgetStack(this);
    62. //add textedit into the QWidgetStack.
    63. textEdit = new QTextEdit(centralWidgetStack,"textedit");
    64. textEdit->setTextFormat(Qt::PlainText);
    65. textEdit->setFocus();
    66. centralWidgetStack->addWidget(textEdit,0);
    67. //add canvasView into the QWidgetStack.
    68. canvas = new QCanvas(this);
    69. canvasView = new CanvasView(canvas,centralWidgetStack,"canvasview");
    70. centralWidgetStack->addWidget(canvasView,1);
    71.  
    72. this->setCentralWidget(centralWidgetStack);
    73. centralWidgetStack->raiseWidget(0);
    74. connect(toolBarStack,SIGNAL(aboutToShow(int)),this,SLOT(switchCentralWidgetMode(int)));
    75.  
    76. }
    77. void MainWindow::switchToolBarMode()
    78. {
    79. if(toolBarMode){
    80. toolBarStack->raiseWidget(1);
    81. }else{
    82. toolBarStack->raiseWidget(0);
    83. }
    84. toolBarMode = !toolBarMode;
    85.  
    86. }
    87.  
    88. void MainWindow::switchCentralWidgetMode(int i)
    89. {
    90. centralWidgetStack->raiseWidget(i);
    91. }
    To copy to clipboard, switch view to plain text mode 

    canvasview.h
    Qt Code:
    1. #ifndef CANVASVIEW_H
    2. #define CANVASVIEW_H
    3. #include <qcanvas.h>
    4.  
    5. class CanvasView:public QCanvasView
    6. {
    7. public:
    8. CanvasView(QCanvas* canvas, QWidget* parent, const char* name);
    9. protected:
    10. void contentsMouseMoveEvent(QMouseEvent* e);
    11.  
    12. };
    13. #endif
    To copy to clipboard, switch view to plain text mode 

    canvasview.cpp
    Qt Code:
    1. #include "canvasview.h"
    2. #include <qcanvas.h>
    3. #include <qapplication.h>
    4. CanvasView::CanvasView(QCanvas* canvas, QWidget* parent, const char* name)
    5. :QCanvasView(canvas,parent,name)
    6. {
    7. setMouseTracking(TRUE);
    8. }
    9.  
    10. void CanvasView::contentsMouseMoveEvent(QMouseEvent* e)
    11. {
    12. qWarning("The MouseMoveEvent has been received!");
    13. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <qapplication.h>
    2. #include "mainwindow.h"
    3. #include "canvasview.h"
    4.  
    5. int main(int argc,char* argv[])
    6. {
    7. QApplication app(argc,argv);
    8. MainWindow* mainwindow = new MainWindow();
    9. app.setMainWidget(mainwindow);
    10. mainwindow->show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setMouseTracking() doesn't work.

    But I'm confused that the contentsMouseMoveEvent() is only called when a mousebutton pressed and moved just as mouseTracking is FALSE.
    Can you please clear about this?

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

    Default Re: setMouseTracking() doesn't work.

    I dont know about Qt3 ...
    but i guess u are overriding the wrong function...
    setMouseTracking(true) enables the widget to receive mouseMoveEvent() ....not contentsMouseMoveEvent ...

    try catching the events in mouseMoveEvent..

    Hope this helps

  4. #4
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: setMouseTracking() doesn't work.

    Quote Originally Posted by vermarajeev View Post
    Can you please clear about this?
    That means, when I hold a mouse button down and move the mouse, the mouseMoveEvent is sent correctly. And then the contentsMouseMoveEvent() in the CanvasView receives the move event correctly too.
    BUT as I mentioned before, I have already set the CanvasView's mouseTracking to TRUE!
    That means, the contentsMouseMoveEvent() can receive mouseMoveEvent even if when mouse move WITHOUT any mouse button pressed! But now the CanvasView can't receive the mouseMoveEvent by mouse moving WITHOUT any mouse button pressed!
    It seems that the code below doesn't work.
    Qt Code:
    1. CanvasView::CanvasView(QCanvas* canvas, QWidget* parent, const char* name) :QCanvasView(canvas,parent,name)
    2. { setMouseTracking(TRUE);}
    To copy to clipboard, switch view to plain text mode 
    That is what I'm confused about.
    Last edited by luffy27; 26th April 2007 at 07:54.

  5. #5
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: setMouseTracking() doesn't work.

    Quote Originally Posted by aamer4yu View Post
    I dont know about Qt3 ...
    but i guess u are overriding the wrong function...
    setMouseTracking(true) enables the widget to receive mouseMoveEvent() ....not contentsMouseMoveEvent ...

    try catching the events in mouseMoveEvent..

    Hope this helps
    But in the QanvasView the contentsMouseMoveEvent is the only function that can receive the mouseMoveEvent.

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

    Default Re: setMouseTracking() doesn't work.

    But in the QanvasView the contentsMouseMoveEvent is the only function that can receive the mouseMoveEvent.
    Are you sure ?? i saw Q3CanvasView and it is derived from QWidget ... isnt QCanvasView derived from QWidget ??
    If it is derived, then u can override the mouseMoveEvent.... try it

  7. #7
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: setMouseTracking() doesn't work.

    The QCanvasView is derived from QScrollView, the contentsMouseMoveEvent() is the member function of QScrollView. And also as the Qt documents example described, when you want to deal with mouseMoveEvent, mouePressEvent, mouseReleaseEvent etc. in the QCanvasView you can directly reimplement the contentsMouse****Event() which are all the member function of the QScrollView.

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

    Default Re: setMouseTracking() doesn't work.

    I'm not sure, but maybe you have to enable the mouse tracking on the viewport widget like in many corresponding situations you have to do in Qt4?
    J-P Nurmi

  9. #9
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: setMouseTracking() doesn't work.

    What does it mean by "enable the mouse tracking on the viewport widget"?
    What is viewport widget? I have only CanvasView wthin a QWidgetStack.

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

    Default Re: setMouseTracking() doesn't work.

    Quote Originally Posted by luffy27 View Post
    What does it mean by "enable the mouse tracking on the viewport widget"?
    What is viewport widget? I have only CanvasView wthin a QWidgetStack.
    See QScrollView::viewport().

    Try:
    Qt Code:
    1. viewport()->setMouseTracking(TRUE);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  11. #11
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: setMouseTracking() doesn't work.

    Could anybody are willing to compile the code and run it ?
    When you run it ,you can click the Graphic button to switch to the QCanvasView. Then you hold the left mouse button dow and move it. Finally you can find the output
    "The MouseMoveEvent has been received!" on the console. That means the QCanvasView receives the mouseMoveEvent correctly.
    But when you just move the mouse without any button pressed down. There is no output on the console. So that means QCanvasView can not receive the mouseMoveEvent in the condition of no mouse button pressed down.

    I hope that you can try it on practice. You will see the problem more clearly.
    Thanks a lot!

  12. #12
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: setMouseTracking() doesn't work.

    Quote Originally Posted by luffy27 View Post
    What does it mean by "enable the mouse tracking on the viewport widget"?
    What is viewport widget? I have only CanvasView wthin a QWidgetStack.
    Quote Originally Posted by jpn View Post
    See QScrollView::viewport().

    Try:
    Qt Code:
    1. viewport()->setMouseTracking(TRUE);
    To copy to clipboard, switch view to plain text mode 
    All right I will try it.

  13. #13
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setMouseTracking() doesn't work.

    Quote Originally Posted by luffy27 View Post
    All right I will try it.
    Qt Code:
    1. viewport()->setMouseTracking(TRUE);
    To copy to clipboard, switch view to plain text mode 
    should work....

  14. #14
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: setMouseTracking() doesn't work.

    YES!That does work!
    Thank you guys very much and best regards!

Similar Threads

  1. QWidget -> updateGeometry dont work.
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 22nd May 2013, 15:55
  2. which ide/editor/database work best with qt4?
    By kkool84 in forum Installation and Deployment
    Replies: 17
    Last Post: 24th August 2006, 06:45
  3. Which version should I work on i686 host for arm target?
    By sliawati in forum Installation and Deployment
    Replies: 4
    Last Post: 11th August 2006, 09:44
  4. QTextEdit Justify align making work
    By dec0ding in forum Qt Programming
    Replies: 2
    Last Post: 13th January 2006, 12:02

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.