PDA

View Full Version : setMouseTracking() doesn't work.



luffy27
26th April 2007, 05:38
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

#include <qdockwindow.h>
#include <qwidgetstack.h>
#include <qtoolbutton.h>
#include <qtextedit.h>
#include <qcanvas.h>
#include "canvasview.h"
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent=0,const char* name = 0);
private slots:
void switchToolBarMode();
void switchCentralWidgetMode(int);
private:
void init();
QWidgetStack* centralWidgetStack;
QDockWindow* toolBarDockWindow;
QWidgetStack* toolBarStack;
QToolButton* graphicButton;
QToolButton* quitButton;
QTextEdit *textEdit;
CanvasView* canvasView;
QCanvas* canvas;
bool toolBarMode;
};
#endif


mainwindow.cpp


#include "mainwindow.h"
#include "canvasview.h"
#include <qmainwindow.h>
#include <qapplication.h>
#include <qlayout.h>

MainWindow::MainWindow(QWidget* parent, const char* name)
:QMainWindow(parent,name)
{
int screenW;
int screenH;
screenW = QApplication::desktop()->width();
screenH = QApplication::desktop()->height();
this->resize(screenW,screenH);
toolBarMode = TRUE;
init();
}

void MainWindow::init()
{
toolBarDockWindow = new QDockWindow(QDockWindow::InDock,this,"toolbardockwindow",0);
toolBarDockWindow->setOrientation(Qt::Horizontal);
toolBarDockWindow->setHorizontallyStretchable(TRUE);
toolBarDockWindow->setVerticallyStretchable(TRUE);
toolBarDockWindow->setMovingEnabled(FALSE);
this->moveDockWindow(toolBarDockWindow,Qt::DockBottom);

toolBarStack = new QWidgetStack(toolBarDockWindow);
toolBarDockWindow->setWidget(toolBarStack);

QWidget* editToolBar = new QWidget(toolBarStack);
QHBoxLayout* editToolBarLayout = new QHBoxLayout(editToolBar,6,6);
QWidget* graphicToolBar = new QWidget(toolBarStack);
QHBoxLayout* graphicToolBarLayout = new QHBoxLayout(graphicToolBar,6,6);
toolBarStack->addWidget(editToolBar,0);
toolBarStack->addWidget(graphicToolBar,1);

graphicButton = new QToolButton(editToolBar,"graphicutton");
graphicButton->setUsesTextLabel(TRUE);
graphicButton->setTextLabel("Graphic",FALSE);
graphicButton->setFont(QFont("Times",20,QFont::Bold));
editToolBarLayout->addWidget(graphicButton);

QSpacerItem* editToolBarSpacer = new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy ::Minimum);
editToolBarLayout->addItem(editToolBarSpacer);
quitButton = new QToolButton(graphicToolBar,"quitbutton");
quitButton->setUsesTextLabel(TRUE);
quitButton->setTextLabel("Return",FALSE);
quitButton->setFont(QFont("Times",20,QFont::Bold));
graphicToolBarLayout->addWidget(quitButton);

QSpacerItem* graphicToolBarSpacer = new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy ::Minimum);
graphicToolBarLayout->addItem(graphicToolBarSpacer);

toolBarStack->raiseWidget(0);

connect(graphicButton,SIGNAL(clicked()),this,SLOT( switchToolBarMode()));
connect(quitButton,SIGNAL(clicked()),this,SLOT(swi tchToolBarMode()));

//construct the QWidgetStack as the MainWindow's main widget.
centralWidgetStack = new QWidgetStack(this);
//add textedit into the QWidgetStack.
textEdit = new QTextEdit(centralWidgetStack,"textedit");
textEdit->setTextFormat(Qt::PlainText);
textEdit->setFocus();
centralWidgetStack->addWidget(textEdit,0);
//add canvasView into the QWidgetStack.
canvas = new QCanvas(this);
canvasView = new CanvasView(canvas,centralWidgetStack,"canvasview");
centralWidgetStack->addWidget(canvasView,1);

this->setCentralWidget(centralWidgetStack);
centralWidgetStack->raiseWidget(0);
connect(toolBarStack,SIGNAL(aboutToShow(int)),this ,SLOT(switchCentralWidgetMode(int)));

}
void MainWindow::switchToolBarMode()
{
if(toolBarMode){
toolBarStack->raiseWidget(1);
}else{
toolBarStack->raiseWidget(0);
}
toolBarMode = !toolBarMode;

}

void MainWindow::switchCentralWidgetMode(int i)
{
centralWidgetStack->raiseWidget(i);
}


canvasview.h


#ifndef CANVASVIEW_H
#define CANVASVIEW_H
#include <qcanvas.h>

class CanvasView:public QCanvasView
{
public:
CanvasView(QCanvas* canvas, QWidget* parent, const char* name);
protected:
void contentsMouseMoveEvent(QMouseEvent* e);

};
#endif


canvasview.cpp


#include "canvasview.h"
#include <qcanvas.h>
#include <qapplication.h>
CanvasView::CanvasView(QCanvas* canvas, QWidget* parent, const char* name)
:QCanvasView(canvas,parent,name)
{
setMouseTracking(TRUE);
}

void CanvasView::contentsMouseMoveEvent(QMouseEvent* e)
{
qWarning("The MouseMoveEvent has been received!");
}


main.cpp


#include <qapplication.h>
#include "mainwindow.h"
#include "canvasview.h"

int main(int argc,char* argv[])
{
QApplication app(argc,argv);
MainWindow* mainwindow = new MainWindow();
app.setMainWidget(mainwindow);
mainwindow->show();
return app.exec();
}

vermarajeev
26th April 2007, 06:53
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?

aamer4yu
26th April 2007, 07:18
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 :)

luffy27
26th April 2007, 07:44
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.

CanvasView::CanvasView(QCanvas* canvas, QWidget* parent, const char* name) :QCanvasView(canvas,parent,name)
{ setMouseTracking(TRUE);}
That is what I'm confused about.

luffy27
26th April 2007, 07:49
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.

aamer4yu
26th April 2007, 08:02
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 :)

luffy27
26th April 2007, 08:28
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.

jpn
26th April 2007, 08:38
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?

luffy27
26th April 2007, 08:48
What does it mean by "enable the mouse tracking on the viewport widget"?
What is viewport widget? I have only CanvasView wthin a QWidgetStack.

jpn
26th April 2007, 08:57
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() (http://doc.trolltech.com/3.3/qscrollview.html#viewport).

Try:


viewport()->setMouseTracking(TRUE);

luffy27
26th April 2007, 09:03
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!

luffy27
26th April 2007, 09:05
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() (http://doc.trolltech.com/3.3/qscrollview.html#viewport).

Try:


viewport()->setMouseTracking(TRUE);


All right I will try it.

vermarajeev
26th April 2007, 11:58
All right I will try it.


viewport()->setMouseTracking(TRUE);
should work....

luffy27
27th April 2007, 18:16
YES!That does work!
Thank you guys very much and best regards!