QPolygonF intersected wrong outcome
Unsolved
General and Desktop
-
Hello !
I encounter a small problem while using the intersected method from the class QPolygonF with Qt5.15.2. And I was wondering how the algortihm works for the polygon intersection since it returns a wrong result, see below:
Cpp file
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QGraphicsScene> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); GraphicsColorPathItem *pathItem1 = new GraphicsColorPathItem(); GraphicsColorPathItem *pathItem2 = new GraphicsColorPathItem(); GraphicsColorPathItem *pathItem3 = new GraphicsColorPathItem(); QPolygonF poly1; QPolygonF poly2; poly1.append(QPointF(72.80599977946001, -20.0)); poly1.append(QPointF(79.4595245353603, -20.0)); poly1.append(QPointF(175.0, -20.0)); poly1.append(QPointF(175.0, 20.0)); poly1.append(QPointF(85.83632192403258, 20.0)); poly1.append(QPointF(72.80599977946001, -20.0)); poly2.append(QPointF(138.90690452319478, -20.0)); poly2.append(QPointF(125.87658237835221, 20.0)); poly2.append(QPointF(0.0, 20.0)); poly2.append(QPointF(-175.0, 20.0)); poly2.append(QPointF(-175.0, -19.999999999999996)); poly2.append(QPointF(132.25337970676924, -19.999999999999996)); poly2.append(QPointF(138.90690452319478, -20.0)); QPolygonF poly3 = poly1.intersected(poly2); pathItem1->setPolygon(poly1); pathItem2->setPolygon(poly2); pathItem1->setBorderColor(QColor(Qt::GlobalColor::blue)); pathItem1->setBorderSize(2); pathItem1->setZValue(2); pathItem2->setBorderColor(QColor(Qt::GlobalColor::red)); pathItem2->setBorderSize(3); pathItem2->setZValue(1); pathItem3->setPolygon(poly3); pathItem3->setBorderColor(QColor(Qt::GlobalColor::yellow)); pathItem3->setBorderSize(1); pathItem3->setZValue(3); QGraphicsScene *scene = new QGraphicsScene(); scene->addItem(pathItem1); scene->addItem(pathItem2); scene->addItem(pathItem3); ui->graphicsView->setScene(scene); ui->graphicsView->setTransform(QTransform(7, 0, 0, 7, 0, 0)); } MainWindow::~MainWindow() { delete ui; }
Header file:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QGraphicsPathItem> #include <QGraphicsPolygonItem> #include <QColor> #include <QPainter> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H class GraphicsColorPathItem : public QGraphicsPolygonItem { public: GraphicsColorPathItem() { m_borderColor = QColor(0,0,0); m_borderSize = 0; } QColor borderColor() const { return m_borderColor; } void setBorderColor(const QColor &newBorderColor) { m_borderColor = newBorderColor; } QRectF boundingRect() const { return this->polygon().boundingRect(); } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override { Q_UNUSED(option) Q_UNUSED(widget) painter->setRenderHint(QPainter::Antialiasing); QPen pen(m_borderColor); pen.setWidth(m_borderSize); painter->setPen(pen); painter->drawPolygon(polygon()); } int borderSize() const { return m_borderSize; } void setBorderSize(int newBorderSize) { m_borderSize = newBorderSize; } private: QColor m_borderColor; int m_borderSize; };
And this is the output:
Is it a known bug from Qt5.15 .2?
Sincerely
-
Probably related to:
https://bugreports.qt.io/browse/QTBUG-75146
https://bugreports.qt.io/browse/QTBUG-100302The conventional wisdom is to not do geometry with Qt. I have had an open patch in gerrit, but it's been pretty much abandonware for the last few years, so if you want to fix this/these I can give you a headstart.