Hi all!

I have a QWidget subclassed and I need to implement some repaints with mouse move event, but when I use the QRect contains to check if the mouse position is inside the QRect it gives me a different position as the drawed QRect.

What is going on here?

Code
fluxocaixawidget.h
Qt Code:
  1. #ifndef FLUXOCAIXAWIDGET_H
  2. #define FLUXOCAIXAWIDGET_H
  3.  
  4. #include <QWidget>
  5. #include <QPainter>
  6.  
  7. class fluxoCaixaWidget : public QWidget
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit fluxoCaixaWidget(QWidget *parent = 0);
  12.  
  13. QRect rectLinhaTempo();
  14.  
  15. protected:
  16. void paintEvent(QPaintEvent *);
  17. void mouseMoveEvent(QMouseEvent *);
  18.  
  19. private:
  20. bool dentro;
  21. QRect linhaTempo;
  22.  
  23. signals:
  24.  
  25. public slots:
  26.  
  27. };
  28.  
  29. #endif // FLUXOCAIXAWIDGET_H
To copy to clipboard, switch view to plain text mode 

fluxocaixawidget.cpp
Qt Code:
  1. #include "fluxocaixawidget.h"
  2.  
  3. #include <QDebug>
  4.  
  5. fluxoCaixaWidget::fluxoCaixaWidget(QWidget *parent) :
  6. QWidget(parent)
  7. {
  8. this->setMouseTracking(true);
  9. this->dentro = false;
  10. }
  11.  
  12. void fluxoCaixaWidget::paintEvent(QPaintEvent *)
  13. {
  14. QPainter painter(this);
  15.  
  16. linhaTempo.setRect(20, this->parentWidget()->height() / 2 - 20, this->parentWidget()->width()-65, 30);
  17.  
  18. QPen caneta;
  19.  
  20. QBrush pincel(Qt::white);
  21.  
  22. painter.setBrush(pincel);
  23.  
  24. painter.drawRect(parentWidget()->rect());
  25.  
  26. if(this->dentro == true)
  27. {
  28. caneta.setColor(Qt::red);
  29. caneta.setWidth(4);
  30. }
  31. else
  32. {
  33. caneta.setColor(Qt::black);
  34. caneta.setWidth(2);
  35. }
  36.  
  37. painter.setPen(caneta);
  38.  
  39. painter.drawRect(linhaTempo);
  40. }
  41.  
  42. void fluxoCaixaWidget::mouseMoveEvent(QMouseEvent *)
  43. {
  44. if(this->linhaTempo.contains(QCursor::pos()))
  45. {
  46. qDebug() << "OK";
  47. this->dentro = true;
  48. }
  49. else
  50. {
  51. qDebug() << "Fora";
  52. this->dentro = false;
  53. }
  54. this->update();
  55. }
To copy to clipboard, switch view to plain text mode