Search code examples
c++qtarduinogpsqpainter

Best way to draw gps point with qt


I dont really know how to ask this question but I will do my best to explain. I have a project on arduino with a gps, I transfere the geo point via serialdata on my pc. I can read it without probleme, but where I need help is how I can draw a path of that point. for the moment I use this:

    void MainWindow::paintEvent(QPaintEvent* p)
    {
        QPainter painter(this); //class must be implemented from QWidget
        painter.setPen(pen);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setRenderHint(QPainter::HighQualityAntialiasing);
        painter.translate(width() / 2-466133, height() / 2+727150);
        painter.scale(10000.0,10000.0);

        painter.drawPolyline(polyPoints);
    }

    void MainWindow::readData(QStringList data)
    {
        ui->textEdit->setText(data.join(","));
        polyPoints  << QPointF(data[0].toDouble(),data[1].toDouble());
        QWidget::update();
    }

my point is something like that:

46.612823, -72.702957
46.612876, -72.702873
46.612937, -72.702789

like you see the difference between 2 point are very very small so I need to scalle this way up and a need to work with rediculous number for translate my origin. for the moment the translate is fixe but in the futur it will be dynamic.

there an image to show more what I need at the end

the style of path I need


Solution

  • You can use QTransform to scale and move the polygon. QPainter scaling will increase the width of the drawing line.

    Use QPolygonF::boundingRect() to get the original points' size and scale from there.

    #include "mainwindow.h"
    
    #include <QPainter>
    #include <QPolygonF>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        this->resize(250, 250);
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    void MainWindow::paintEvent(QPaintEvent *)
    {
        QPolygonF polygon_;
        polygon_ << QPointF{46.61, -72.7};
        polygon_ << QPointF{46.62, -73};
        polygon_ << QPointF{46.63, -72.78};
    
        QPainter painter(this);
        QPen pen;
        pen.setWidthF(1);
        pen.setColor(QColor(Qt::red));
        painter.setPen(pen);
        painter.setRenderHint(QPainter::Antialiasing);
    
        QRectF rect = polygon_.boundingRect();
    
        qreal scale_x = this->width() / rect.width();
        // Qt's y axis is positive downward
        qreal scale_y = - this->height() / rect.height();
    
        // Use rect's after-scaling properties to calculate
        qreal left_border = (this->width() - rect.width() * scale_x) / 2;
        qreal top_border = (this->height() - rect.height() * scale_y) / 2;
    
        // Use rect's after-scaling properties to calculate
        QTransform transform;
        transform.translate(-rect.x() * scale_x + left_border,
                            -rect.y() * scale_y + top_border);
        transform.scale(scale_x, scale_y);
        painter.drawPolyline(transform.map(polygon_));
    }
    

    Output:

    enter image description here