Enter/Leave events are not emitted



  • Greetings.

    I've made my own custom widget to embed Ogre into Qt. It's a simple QWidget from which I get the winId and pass it to Ogre. It works pretty well, except for one thing: I don't always get the enter and leave events when the mouse enters and leaves it (I would say I get them one time out of ten). The underMouse() method also always returns false. However the mouse move event works perfectly.

    Here is the (minimal) code:
    @
    class OgreWidget : public QWidget
    {
    Q_OBJECT

    private:
    OgreStuff* mOgre;
    Ogre::RenderWindow* mRenderWindow;
    Ogre::Viewport* mViewport;
    Ogre::Camera* mCamera;

    void initOgre();
    
    virtual QPaintEngine* paintEngine() const;
    virtual void showEvent(QShowEvent* evt);
    virtual void paintEvent(QPaintEvent* evt);
    virtual void resizeEvent(QResizeEvent* evt);
    virtual void mouseMoveEvent(QMouseEvent* evt);
    virtual void leaveEvent(QEvent* evt);
    

    public:
    OgreWidget(QWidget* parent = 0, Qt::WindowFlags f = 0);
    ~OgreWidget();
    };

    OgreWidget::OgreWidget(QWidget* parent, Qt::WindowFlags f)
    : QWidget(parent, f)
    {
    setContextMenuPolicy(Qt::DefaultContextMenu);
    setFocusPolicy(Qt::WheelFocus);
    setMouseTracking(true);
    setAttribute(Qt::WA_PaintOnScreen);
    setAttribute(Qt::WA_NoSystemBackground);
    }

    OgreWidget::~OgreWidget()
    {
    delete mOgre;
    }

    void OgreWidget::initOgre()
    {
    // Create Ogre
    mOgre = new OgreStuff();
    // Get id of the existing window created by Qt
    Ogre::NameValuePairList params;
    params["parentWindowHandle"] = Ogre::StringConverter::toString((unsigned long)winId());
    // Create render window
    mRenderWindow = mOgre->root->createRenderWindow("MyWindow",
    width(),
    height(),
    false,
    &params);
    // Create viewport
    mViewport = mRenderWindow->addViewport(0, 0);
    }

    QPaintEngine* OgreWidget::paintEngine() const
    {
    return 0;
    }

    void OgreWidget::showEvent(QShowEvent* evt)
    {
    if (!mOgre)
    {
    QApplication::flush();
    initOgre();
    }
    QWidget::showEvent(evt);
    setFocus();
    }

    void OgreWidget::paintEvent(QPaintEvent* evt)
    {
    mOgre->root->_fireFrameStarted();
    mRenderWindow->update();
    mOgre->root->_fireFrameRenderingQueued();
    mOgre->root->_fireFrameEnded();
    }

    void OgreWidget::resizeEvent(QResizeEvent* evt)
    {
    if (!mRenderWindow)
    return;
    mRenderWindow->resize(width(), height());
    mRenderWindow->windowMovedOrResized();
    }

    void OgreWidget::leaveEvent(QEvent* evt)
    {
    cout << "LEAVE" << endl;
    evt->accept();
    }

    void OgreWidget::mouseMoveEvent(QMouseEvent* evt)
    {
    cout << "MOVE" << endl;
    evt->accept();
    }
    @

    If I comment this one line
    @params["parentWindowHandle"] = Ogre::StringConverter::toString((unsigned long)winId());@
    then Ogre renders into its own window outside of Qt and then the leave and enter events on my widget works perfectly. So this is definitely the cause of the problem. I couldn't find any solution, any idea?

    I'm on Ubuntu, I didn't try it on Windows and Mac yet. I'm using Qt 4.8.4.

    Regards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.