[Qt-interest] QWidget::enterEvent and QWidget::leaveEvent

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Mon Jun 1 06:51:35 CEST 2009


Hi Samual, 

When you override virtual methods, you need to double check the types,
otherwise C++ just thinks you are creating new methods.  The doco for
enterEvent shows: 

void QWidget::enterEvent ( QEvent * event )

not QMouseEvent as you have it. 

Not sure about your second question. 

Hope this helps, 



> In the attached program (consisting of the two files main.cpp and
> test_enter_event.h ), the functions
>     void enterEvent(QMouseEvent *)
>     void leaveEvent(QMouseEvent *)
> are overridden to record whether the mouse cursor is currently inside
> the widget (a QGLWidget, to be specific). Yet neither function is
> called when the mouse moves in or out of the window/widget.
> The line should cease to be drawn when the mouse exits the window.
> 
> The commented out line, which tests whether the geometry includes the
> current mouse location, also does not work.
> 
> Do you see why the (more efficient) first method does not work? Out of
> curiosity, any ideas why the second method does not work are also
> welcome.
> 
> Qt 4.4, Mac OS X 10.5, g++ 4.0.1.
> 
> TIA,
> Samuel
> 
> ----------------main.cpp----------------
> #include <QApplication>
> #include <iostream>
> 
> #include "test_enter_event.h"
> 
> int main(int argc, char *argv[])
> {
>     QApplication app(argc, argv);
> 
>     if (!QGLFormat::hasOpenGL()) {
>         std::cerr << "This system has no OpenGL support" << std::endl;
>         return 1;
>     }
> 
>     Test_Enter_Event test_enter_event;
>     test_enter_event.setWindowTitle(QObject::tr("Test enterEvent"));
>     test_enter_event.resize(640, 480);
>     test_enter_event.show();
> 
>     return app.exec();
> }
> 
> ----------------test_enter_event.h----------------
> 
> #ifndef TEST_ENTER_EVENT_H
> #define TEST_ENTER_EVENT_H
> 
> #include <QtGui>
> #include <QGLWidget>
> 
> class Test_Enter_Event : public QGLWidget
> {
>     Q_OBJECT
> 
> public:
>     Test_Enter_Event(QWidget *parent = 0) : QGLWidget(parent),
>                                             mouse_is_inside(false)
>     {
>         setFormat(QGLFormat(QGL::DoubleBuffer));
>         setMouseTracking(true);
>     }
> 
> 
> protected:
>     void initializeGL() {
>         glDisable (GL_LIGHTING);
>     }
> 
>     void resizeGL(int width, int height) {
>         glViewport (0, 0, (GLsizei) width, (GLsizei) height);
>         glMatrixMode (GL_PROJECTION);
>         glLoadIdentity();
>         glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
>         window_height = height;
> 
>         glMatrixMode(GL_MODELVIEW);
>         glLoadIdentity();
>     }
> 
>     void paintGL() {
>         glClearColor (0.1, 0.2, 0.5, 1.0);
>         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
> 
>         if( mouse_is_inside ) {
>             glColor3f(1.0, 1.0, 1.0);
>             glBegin(GL_LINES);
>             glVertex2d(0.0, 0.0);
>             glVertex2d(mouse.x(), mouse.y());
>             glEnd();
> 
>             glFlush ();
>         }
>     }
>     void mouseMoveEvent(QMouseEvent * event) {
> 
>         mouse = QPoint( event->x(),
>                         window_height - event->y() );
> //    mouse_is_inside = geometry().contains(mouse_x, mouse_y);
>         mouse_is_inside = true;
>         update();
>     }
> 
> 
>     void enterEvent(QMouseEvent *) {
>         mouse_is_inside = true;
>     }
>     void leaveEvent(QMouseEvent *) {
>         mouse_is_inside = false;
>     }
> 
> 
> private:
>     void draw();
>     bool mouse_is_inside;
>     int window_height;
>     QPoint mouse;
> };
> 
> #endif // TEST_ENTER_EVENT_H
> 
> ----------------------------------------------------------------
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
> 
> __________ Information from ESET NOD32 Antivirus, version of 
> virus signature database 4117 (20090530) __________
> 
> The message was checked by ESET NOD32 Antivirus.
> 
> http://www.eset.com
> 
> 
> 




More information about the Qt-interest-old mailing list