November 10, 2011

yan bellavance yan bellavance
Lab Rat
50 posts

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:

  4. #include <QWidget>
  5. #include <QObject>
  6. #include <QSpinBox>
  7. #include <QEvent>
  8. #include <QMouseEvent>
  11. class wvSpinBoxMsnEdit : public QSpinBox
  12. {
  13. Q_OBJECT
  15. public:
  16.     explicit wvSpinBoxMsnEdit(QWidget *parent = 0);
  18. protected:
  19.     void mousePressEvent(QMouseEvent *event);
  20.     void mouseReleaseEvent(QMouseEvent *event);
  22. public:
  23.     bool msnEdit;
  25. };
  27. #endif // WVSPINBOXMSNEDIT_H

and here is my cpp:

  1. #include "wvspinboxmsnedit.h"
  3. wvSpinBoxMsnEdit::wvSpinBoxMsnEdit(QWidget *parent) :
  4.     QSpinBox(parent)
  5. {
  6.     msnEdit=false;
  7. }
  8. void wvSpinBoxMsnEdit::mousePressEvent ( QMouseEvent * event ){
  9.     msnEdit=true;
  10.     QAbstractSpinBox::mousePressEvent(event);
  11.     event->accept();
  12. }
  14. void wvSpinBoxMsnEdit::mouseReleaseEvent ( QMouseEvent * event ){
  15.     msnEdit=true;
  16.     QAbstractSpinBox::mouseReleaseEvent(event);
  17.     event->accept();
  18. }


1 reply

November 18, 2011

BilbonSacquet BilbonSacquet
Lab Rat
75 posts

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!

  ‹‹ ComboBox Max Amount of Items?      How to set Active Tab in DockWidget? ››

You must log in to post a reply. Not a member yet? Register here!