我试图使用painterpath方法在小部件上进行自由绘制。所以在mouseMove事件中我试图找到使用的交集 qpainterPath.intersects(masterPainterPath) intersects 但 当我在未封闭区域内移动或朝向绘制路径上的任何点移动时,画家路径的交叉返回真实
你能看到第二张图片..甚至端点(蓝线)也没有穿过实际路径..但画家路径相交正在返回
文件说,事件 在路径上设置操作会将路径视为区域。非封闭路径将被视为隐式关闭
蓝线代表相交的线。
这是我的代码
#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();
}
我已经附加了正常情况和我遇到问题的情况。
交叉点发生后的调试消息完整点 在主列表上是: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)) -------------------------------------------------- ----- poly上的完整点:QPolygonF(QPointF(463,155)QPointF(463,154) QPointF(463,155))
答案 0 :(得分:1)
newPath.intersects(masterPath)
始终为真,因为情况属实。
而不是使用newPath.intersects(masterPath)
计算交叉路径并从该路径中删除第一个端点。然后测试路径是否为空。
像
这样的东西interPath = newPath.intersected (masterPath );
interPath -= QPainterPath(lastPoint); //test first without this line
if(interPath.empty())
{
...
}
对于实际解决方案,您需要一点点误差。您应该在newPath
的开头处创建一个小圆圈路径,而不是QPainterPath(lastPoint)
;