Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPaintEvent
Qt 6.11 is out! See what's new in the release blog

QPaintEvent

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 348 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kingJulian
    wrote on last edited by
    #1

    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 .

    jsulmJ 1 Reply Last reply
    0
    • K kingJulian

      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 .

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @kingJulian said in QPaintEvent:

      because my function getlat() is only getting called twice

      How did you check this?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      0
      • jsulmJ jsulm

        @kingJulian said in QPaintEvent:

        because my function getlat() is only getting called twice

        How did you check this?

        K Offline
        K Offline
        kingJulian
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kingJulian
          wrote on last edited by
          #4

          problem solved

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved