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
#ifndef FLUXOCAIXAWIDGET_H
#define FLUXOCAIXAWIDGET_H
#include <QWidget>
#include <QPainter>
class fluxoCaixaWidget
: public QWidget{
Q_OBJECT
public:
explicit fluxoCaixaWidget
(QWidget *parent
= 0);
protected:
private:
bool dentro;
signals:
public slots:
};
#endif // FLUXOCAIXAWIDGET_H
#ifndef FLUXOCAIXAWIDGET_H
#define FLUXOCAIXAWIDGET_H
#include <QWidget>
#include <QPainter>
class fluxoCaixaWidget : public QWidget
{
Q_OBJECT
public:
explicit fluxoCaixaWidget(QWidget *parent = 0);
QRect rectLinhaTempo();
protected:
void paintEvent(QPaintEvent *);
void mouseMoveEvent(QMouseEvent *);
private:
bool dentro;
QRect linhaTempo;
signals:
public slots:
};
#endif // FLUXOCAIXAWIDGET_H
To copy to clipboard, switch view to plain text mode
fluxocaixawidget.cpp
#include "fluxocaixawidget.h"
#include <QDebug>
fluxoCaixaWidget
::fluxoCaixaWidget(QWidget *parent
) :{
this->setMouseTracking(true);
this->dentro = false;
}
{
linhaTempo.setRect(20, this->parentWidget()->height() / 2 - 20, this->parentWidget()->width()-65, 30);
painter.setBrush(pincel);
painter.drawRect(parentWidget()->rect());
if(this->dentro == true)
{
caneta.setColor(Qt::red);
caneta.setWidth(4);
}
else
{
caneta.setColor(Qt::black);
caneta.setWidth(2);
}
painter.setPen(caneta);
painter.drawRect(linhaTempo);
}
{
if(this
->linhaTempo.
contains(QCursor::pos())) {
qDebug() << "OK";
this->dentro = true;
}
else
{
qDebug() << "Fora";
this->dentro = false;
}
this->update();
}
#include "fluxocaixawidget.h"
#include <QDebug>
fluxoCaixaWidget::fluxoCaixaWidget(QWidget *parent) :
QWidget(parent)
{
this->setMouseTracking(true);
this->dentro = false;
}
void fluxoCaixaWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
linhaTempo.setRect(20, this->parentWidget()->height() / 2 - 20, this->parentWidget()->width()-65, 30);
QPen caneta;
QBrush pincel(Qt::white);
painter.setBrush(pincel);
painter.drawRect(parentWidget()->rect());
if(this->dentro == true)
{
caneta.setColor(Qt::red);
caneta.setWidth(4);
}
else
{
caneta.setColor(Qt::black);
caneta.setWidth(2);
}
painter.setPen(caneta);
painter.drawRect(linhaTempo);
}
void fluxoCaixaWidget::mouseMoveEvent(QMouseEvent *)
{
if(this->linhaTempo.contains(QCursor::pos()))
{
qDebug() << "OK";
this->dentro = true;
}
else
{
qDebug() << "Fora";
this->dentro = false;
}
this->update();
}
To copy to clipboard, switch view to plain text mode
Bookmarks