即時試圖使上使用painterpath方法窗口小部件的自由牽引。所以在mouseMove事件我試圖找到使用 qpainterPath.intersects(masterPainterPath) intersects 但 當我的未閉合區域內部或朝向吸入路徑上的任何點移動的交點,painterpath的交叉被返回true相交繪製painterpath
你可以看到第二圖像..甚至有端點(藍線)沒有橫穿過實際路徑..但painterpath相交時返回true
事件的文檔說 的路徑上設置的操作將對待p作爲區域。非封閉路徑將視爲隱式關閉
藍色線代表相交線。
這是我的代碼
#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( (444,133)QPointF(442,134)QPointF(444, 134)QPointF(446,133)QPo INTF(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) (473,138)QPointF(474,139)QPointF(479,136)QPointF(470,139)QPointF(470,136)QPointF(471,136)QPointF(473,137)QPointF(474,139)QPointF(475,139) QPointF )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)(469,155)QPointF(469,153)QPointF(469,153)QPointF(468,154)QPointF(467,154)QPointF(467, 155)QPointF(466,155)QPointF(465,155)QPointF(464,155) QPointF(422,141 )) ----------------------------------------------- --------在聚完整點:QPolygonF(QPointF(463,155)QPointF(463,154) QPointF(463,155))
即使有圖像,也很難遵循你正在嘗試做的事情。編輯你的第一段,使其更容易理解。 – UmNyobe 2015-02-24 08:50:08
你能看到第二個圖像嗎?即使端點(藍線)沒有穿過實際路徑..但畫家路徑相交是返回true。 – Wagmare 2015-02-24 09:49:39
可能是因爲新路徑的起始點與主路徑的最後一點相交。檢查它們是否連接,然後不要將它們視爲相交。 – Nejat 2015-02-24 10:01:24