Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPaintEvent



  • Hi, am trying to draw points on my image with different colors.
    he is the code

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<iostream>

    using namespace std;

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::MainWindow)

    {
    ui->setupUi(this);

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    /this function get the value of lat/
    double MainWindow::getLat(double k)
    {
    double value=0;

    if(k<=505&&k>=0)
         { value=90-k*(18/101);}
    
    if(k>505&&k<=1010)
          {value=k*(18/101)-90;}
    return value;
    

    }

    void MainWindow:: paintEvent(QPaintEvent *event)
    {

    /*here i plot my image first */

    QImage image;
    image.load("C:/Users/hp/Documents/Qt/untitled2/earthh.jpg");
    QPainter imagePainter(this);
    imagePainter.begin(this);
    imagePainter.drawImage(QPoint(0, 0), image);
    imagePainter.end();
    

    /then here i plot my points/

    QPainterPath dotCircle;
    double lat=0;
    
    for ( double i=0;i<=1920;i+=50)
    {
        for(double j=0;j<1000;j+=50)
        {
    
             dotCircle.addEllipse(QPoint(i,j),3,3);
             QPainter ellipsePathPainter;
             ellipsePathPainter.begin(this);
             ellipsePathPainter.setPen(QPen(Qt::transparent,1, Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
    
            double k=lat;
            lat=qDegreesToRadians(getLat(j)); //the problem is here this line is only getting executed twice when j<550 and when j>505 
    
            double alt=0.0;
            double  champs=(U*M*sqrt(4-3*pow(cos(lat),2)))/(4*M_PI*pow(alt+Re,3))*1000000;
    
            if(champs>=45&&champs<60)
                     {ellipsePathPainter.setBrush(Qt::green);}
            else if(champs>=35&&champs<45)
                     { ellipsePathPainter.setBrush(Qt::yellow);}
            else if(champs>=30&&champs<35)
                     { ellipsePathPainter.setBrush(Qt::magenta);}
            else
                    { ellipsePathPainter.setBrush(Qt::red);}
    
            ellipsePathPainter.drawPath(dotCircle);
            ellipsePathPainter.end();
    
            }
    }
    

    }

    as the outcome the points are plotted all with the same colors because my function getlat() is only getting called twice .


  • Qt Champions 2019

    @kingJulian said in QPaintEvent:

    because my function getlat() is only getting called twice

    How did you check this?



  • @jsulm i printed the value of lat and the outcome was 1.507 and -1.507 multiple times



  • problem solved


Log in to reply