Reputation: 1406
im trying to make a free draw on the widget using the painterpath method . so in mouseMove event i tried to find the intersection using qpainterPath.intersects(masterPainterPath) intersects but when i was moving inside the unclosed area or towards any point on the drawn path , the intersect of painterpath is returning true
can you see the second image .. there even the endpoint (blue line) is not crossing the actual path .. but the painterpath intersect is returning true
event the docs says Set operations on paths will treat the paths as areas. Non-closed paths will be treated as implicitly closed
the blue line represents the intersected line.
this is my code
#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QMessageBox>
#include <QLine>
#include <QDebug>
class MousePaintWidget : public QWidget
{
public:
explicit MousePaintWidget(QWidget * parent = 0)
: QWidget(parent)
, mousePressed(false)
{
;
}
protected:
void paintEvent(QPaintEvent *)
{
QPainter p(this);
p.drawPath(masterPath);
if(mousePressed)
{
p.setPen(Qt::red);
QLineF line = QLineF(firstPoint, lastPoint);
p.drawLine(line);
}
p.setPen(QPen(Qt::blue,4));
p.drawPath(newerPath);
}
void mouseMoveEvent(QMouseEvent * mouseEvent)
{
if(mousePressed)
{
QPainterPath newPath;
newPath.moveTo(lastPoint);
newPath.lineTo(mouseEvent->pos());
if(masterPath.elementCount() == 0)
{
masterPath.moveTo(lastPoint);
}
else
{
if(newPath.intersects(masterPath))
{
newerPath.moveTo(lastPoint);
newerPath.lineTo(mouseEvent->pos());
// mousePressed = false;
masterPath.lineTo(lastPoint);
}
else
{
masterPath.lineTo(lastPoint);
}
}
lastPoint = mouseEvent->pos();
update();
}
}
void mousePressEvent(QMouseEvent * mouseEvent)
{
firstPoint = lastPoint = mouseEvent->pos();
masterPath = QPainterPath();
mousePressed = true;
}
void mouseReleaseEvent(QMouseEvent *)
{
mousePressed = false;
}
private:
bool mousePressed;
QPainterPath masterPath;
QPoint lastPoint;
QPoint firstPoint;
QPainterPath newerPath;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MousePaintWidget w;
w.show();
return app.exec();
}
and i have attached the normal condition and the one which i have the problem with it.
the debug message after the intersection happened The complete points on the master list is: QPolygonF(QPointF(422, 141) QPointF(423, 140) QPointF(425, 139) QPointF(427, 139) QPointF(428, 139) QPointF(429, 138) QPointF(431, 137) QPointF(433, 136) QPointF(436, 136) QPointF(437, 135) QPointF(440, 135) QPointF(442, 134) QPointF(444, 134) QPointF(446, 133) QPointF(448, 133) QPointF(450, 133) QPointF(451, 133) QPointF(452, 133) QPointF(455, 133) QPointF(456, 133) QPointF(457, 133) QPointF(459, 133) QPointF(460, 133) QPointF(461, 133) QPointF(463, 133) QPointF(464, 134) QPointF(465, 134) QPointF(466, 134) QPointF(467, 134) QPointF(469, 135) QPointF(469, 136) QPointF(470, 136) QPointF(471, 136) QPointF(472, 137) QPointF(473, 138) QPointF(474, 139) QPointF(475, 139) QPointF(475, 140) QPointF(475, 142) QPointF(475, 143) QPointF(475, 144) QPointF(475, 145) QPointF(475, 146) QPointF(474, 146) QPointF(474, 147) QPointF(473, 148) QPointF(472, 149) QPointF(471, 151) QPointF(470, 151) QPointF(470, 152) QPointF(469, 152) QPointF(469, 153) QPointF(468, 154) QPointF(467, 154) QPointF(467, 155) QPointF(466, 155) QPointF(465, 155) QPointF(464, 155) QPointF(422, 141) ) ------------------------------------------------------- The complete points on the poly: QPolygonF(QPointF(463, 155) QPointF(463, 154) QPointF(463, 155) )
Upvotes: 1
Views: 741
Reputation: 22890
newPath.intersects(masterPath)
is always true because it is the case.
Rather than using newPath.intersects(masterPath)
compute the intersection path and remove the first end point from that path. Then test if the path is empty.
Something like
interPath = newPath.intersected (masterPath );
interPath -= QPainterPath(lastPoint); //test first without this line
if(interPath.empty())
{
...
}
For a practical solution you need a small margin of error. You should create a small circle path around the beginning of newPath
instead of a QPainterPath(lastPoint)
;
Upvotes: 1