PDA

View Full Version : Mouse Tracking doesnt work from QCanvasView?



hgsw
4th January 2007, 01:07
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):


//main.cpp

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

QApplication *pApp;

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
pApp = &app;
MainWindow mainWin(0);
app.setMainWidget(&mainWin);
mainWin.show();
//mainWin.initCanvas();
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
return app.exec();
}


////////////////////////////////////////////////////////////////////////////////////////////
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <qobject.h>
#include <qmainwindow.h>
#include <qcanvas.h>

#include "myview.h"

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent);
void initCanvas();
myview *pmyview;
QCanvas *pcanvas;

protected:
virtual void closeEvent(QCloseEvent *event);
};
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//mainwindow.cpp
#include <iostream>
#include <qapplication.h>
#include <qcanvas.h>

#include "mainwindow.h"
#include "myview.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
initCanvas();
}

void MainWindow::initCanvas()
{
pmyview = new myview(this);
pcanvas = new QCanvas(640,480);
pmyview->setCanvas(pcanvas);
setCentralWidget(pmyview);
pmyview->show();

//this->pmyview->viewport()->setMouseTracking(true);
//this->pmyview->setMouseTracking(true);
//this->setMouseTracking(true);
//setMouseTracking(true);
}


void MainWindow::closeEvent(QCloseEvent *event)
{
event->accept();
close();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef MYVIEW_H
#define MYVIEW_H

#include <qobject.h>
#include <qcanvas.h>

class myview : public QCanvasView
{
Q_OBJECT

public:
myview(QWidget *parent);

protected:
virtual void mouseMoveEvent( QMouseEvent * e);

};
///////////////////////////////////////////////////////////////////////////////////////////////////////////

//myview.cpp
#include <iostream>
#include "myview.h"

myview::myview(QWidget *parent) : QCanvasView(parent)
{
setMouseTracking(true);
}

void myview::mouseMoveEvent( QMouseEvent * e)
{
std::cout << "moving" << std::endl;
}

wysota
4th January 2007, 01:59
You have to enable tracking for the viewport, not for the view.