1

QMouseEvent stores an integer value of the mouse position. However, it has a protect member "s" which stores a float value of the mouse position. How can I get the float value?

I have tried inheriting the QMouseEvent, but unfortunately I get this error message all the time.

error: C2511: 'QMouseEventF::QMouseEventF(QWidget *)' : overloaded member function not found in 'QMouseEventF'

This is my header file:

#ifndef QMOUSEEVENTF_H
#define QMOUSEEVENTF_H

#include<QMouseEvent>

class QMouseEventF : QMouseEvent
{
    Q_OBJECT

    public:
    QMouseEventF(QObject* parent = 0);

    ~QMouseEventF();
    qreal GetX();

};

#endif // QMOUSEEVENTF_H

And here is the inherited class:

#include "qmouseeventf.h"


QMouseEventF::QMouseEventF(QWidget *parent ): QMouseEvent(parent)
{

}


QMouseEventF::~QMouseEventF()
{

}


qreal QMouseEventF::GetX()
{
    return this->s.rx();
}
tux3
  • 7,171
  • 6
  • 39
  • 51
Nai
  • 71
  • 7
  • 1
    What about QMouseEvent::windowPos() and QMouseEvent::screenPos()? They are public – demonplus Apr 13 '15 at 19:16
  • You are right, but they are integer value where I need the float value. The float value is stored in a protected member. – Nai Apr 13 '15 at 19:20
  • Both return QPointF http://doc.qt.io/qt-5/qpointf.html#setX. This is floating point precision – demonplus Apr 13 '15 at 19:22
  • Thank you, I have tried using QPointF. It is used to store and return a float point of the mouse position. but unfortunately, even though it is defined as a float type, in reality it stored an integer value . – Nai Apr 13 '15 at 20:20
  • 1
    It is completely normal since it is pixels. I am sure if you check in debugger your protected member s will have similar value. – demonplus Apr 14 '15 at 03:36
  • I have done, it has a floating point precision (3 digits), – Nai Apr 14 '15 at 05:23

1 Answers1

1

For one, you have a different signature between header and source file because the header constructor is different than the source constructor. QMouseEvent does not inherit from QObject or QWidget.

Second, QMouseEvent does not take a QWidget * for a constructor.

Third, there is no need for the Q_OBJECT macro in the header.

Those are the reasons for correctness of the code. To answer your original question, it wouldn't make sense to use a float value since the integer value is what the mouse events operate with for pixel coordinates. If you need to convert it to float, do so yourself by casting.

spellmansamnesty
  • 469
  • 4
  • 10
  • Thank you, your advice helps me solving the error message, but in the same time it causes another issue! The mouseMoveEvent now does NOT fired! – Nai Apr 13 '15 at 20:13