2

I catch the mouseMoveEvent of my QWebView for restarting a timer of a screensaver. The problem is that now the mouseMoveEvent isnt distribued to the HTML elements so its impossible for example to move a sliding bar on the page.

I use connect to bind mouseMoveEvent to the restart slot :

QObject::connect(_view, SIGNAL(mouseMoveEvent(QMouseEvent*)), _mediaPlayer, SLOT(stop()));

WebView class :

class WebView : public QWebView
{
    Q_OBJECT
public:

    WebView(QString menu) : _menuDesc(menu) {};
    WebView(){};

    void            setMenuDesc(QString menu) {_menuDesc = menu;};
    QString         getMenuDesc() {return _menuDesc;};
    void            setCurrPage(QString page) {_currPage = page;};
    QString         getCurrPage() {return _currPage;};
    void            setCurrCategory(QString page) {_currPage = page;};
    QString         getCurrCategory() {return _currPage;};

    void            mouseMoveEvent(QMouseEvent *)
    {
        emit mouseMoved();
    };

signals :
    void mouseMoved();

private:

    QString             _menuDesc = 0;
    QString             _currPage;
    QString             _currCategory = 0;
};

Is there a solution to still catch the signal and pass it to the HTML page?

Robin
  • 129
  • 9

2 Answers2

2

mouseMoveEvent is not a signal but an event handler. You can reimplement this event handler and let it emit a signal you can connect to if you need that.

Like this:

MyWebView::mouseMoveEvent(QMouseEvent * e) {
   emit mouseMoved(); // this would be signal you could connect to.
}
Aaron
  • 1,181
  • 7
  • 15
  • Okay i did as you said. WebView class: `void mouseMoveEvent(QMouseEvent *) { emit mouseMoved(); }; signals : void mouseMoved(); ` Then i connect to the mouseMoved signal: `QObject::connect(_view, SIGNAL(mouseMoved()), _mediaPlayer, SLOT(stop()));`. Is that correct ? The event is not still passed to the html page. – Robin Dec 10 '15 at 11:01
  • Did you also click ?. As long as you do not activate mouse tracking a move event will only be triggered between mouse press and mouse release events. You could try the `mousePressEvent` for now. – Aaron Dec 10 '15 at 11:09
  • And `_view` is of type `WebView` which you derived from QWebView ? And you added the Q_OBJECT macro to the private section ? – Aaron Dec 10 '15 at 11:10
  • check with `QDebug` whether your mouseMoveEvent() method gets called. – Aaron Dec 10 '15 at 11:28
  • I click my bar and (still holding left click) i try to pull it down. I edited the post to put my WebView class declaration. I tried with `mousePressEvent` too but i need now a double click to reach the page elements. The first one does nothing. – Robin Dec 10 '15 at 11:30
2

Seems you misunderstand event handler and signal usages.

mouseMoveEvent is a member method of QWidget, is not a signal so you cannot connect to it. You can override it in you subclass and emit your own signal.

And if a QWidget's mouse tracking is switched off, mouse move events only occur if a mouse button is pressed while the mouse is being moved. Maybe you need to call setMouseTracking too.

hiitiger
  • 545
  • 4
  • 13
  • I tried @Aaron answer and setting mouse tracking to true, but cant make my bar slide atm, it works well when i get rid of the mouseMoveEvent override in my class. – Robin Dec 10 '15 at 11:18
  • @Robin, in your subclass's mouseMoveEvent code, call QWebView::mouseMoveEvent(event) before or after emit the signal, or the base class's handler won't get excuted. – hiitiger Dec 10 '15 at 11:29
  • Okay i see, sounds logical and works :). Thank you all ! – Robin Dec 10 '15 at 11:57