MousePressEvent ( QMouseEvent * event ) in subclass of QSpinbox getting called only when cursor on up down arrow



  • I am trying to re-implement the mousePressEvent in a sub class of QSpinBox but it only gets called when the mouse interacts with the up and down arrows of the spinbox. Is there a way I can get around this? Perhaps and event filter...gonna look into that.

    here is my header:
    @#ifndef WVSPINBOXMSNEDIT_H
    #define WVSPINBOXMSNEDIT_H

    #include <QWidget>
    #include <QObject>
    #include <QSpinBox>
    #include <QEvent>
    #include <QMouseEvent>

    class wvSpinBoxMsnEdit : public QSpinBox
    {
    Q_OBJECT

    public:
    explicit wvSpinBoxMsnEdit(QWidget *parent = 0);

    protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);

    public:
    bool msnEdit;

    };

    #endif // WVSPINBOXMSNEDIT_H
    @

    and here is my cpp:

    @#include "wvspinboxmsnedit.h"

    wvSpinBoxMsnEdit::wvSpinBoxMsnEdit(QWidget *parent) :
    QSpinBox(parent)
    {
    msnEdit=false;
    }
    void wvSpinBoxMsnEdit::mousePressEvent ( QMouseEvent * event ){
    msnEdit=true;
    QAbstractSpinBox::mousePressEvent(event);
    event->accept();
    }

    void wvSpinBoxMsnEdit::mouseReleaseEvent ( QMouseEvent * event ){
    msnEdit=true;
    QAbstractSpinBox::mouseReleaseEvent(event);
    event->accept();
    }
    @

    thanks



  • Of course a QSpinBox widget is a composition of widgets, in fact 2 widgets the spinbox and it's child the line edit) and the rule is that the upper widget get the mouse event.

    In case of the spinbox it's the text line widget which takes them. The buttons in contrario aren't child widgets it's just a graphical part of the spinbox. it's rendered by the style (QXxxStyle).

    To resume if you go on text edit the mouse messages go to it, other areas it is its parent - the combobox which gets them.

    Now of course you should install an event filter to the line edit but it sound all like an hack, rethink more on the way you will implement and probably there is another to handle your problem without to take the mouse events!


Log in to reply
 

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