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. How to draw with vertices, circle (and other shapes) ?

How to draw with vertices, circle (and other shapes) ?

Scheduled Pinned Locked Moved Solved General and Desktop
2d graphicspainter
6 Posts 2 Posters 570 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.
  • timob256T Offline
    timob256T Offline
    timob256
    wrote on last edited by
    #1

    I drew a circle.
    But for some reason he is twitchy, as if he was drawn by a man without a dividers.
    I think this is because drawPoint takes an int. And I have a float vector .

    here is all the code :

    **krug_gt.h**
    
    #ifndef KRUG_QT_H
    #define KRUG_QT_H
    
    #include <QMainWindow>
    #include <QDebug>
    #include <QPainter>
    #include <QPen>
    #include <QFont>
    #include <QtMath>
    
    
    class Krug_qt : public QMainWindow
    {
        Q_OBJECT
    
    public:
        Krug_qt(QWidget *parent = 0);
        ~Krug_qt();
    
        void fillVertexArray();
    
        float R;        // радиус
        int p;          // Количество продольных срезов. Number of longitudinal slices.
        int q;          // Количество широтных срезов. Number of latitudinal slices.
        int kol_toch;
    
        QVector<float> integerVector;
    
    
    protected:
        void paintEvent(QPaintEvent *event);
    //     void keyPressEvent(QKeyEvent *event);
    };
    
    #endif // KRUG_QT_H
    

    kruq_qt.cpp

    #include "krug_qt.h"
    
    Krug_qt::Krug_qt(QWidget *parent)
        : QMainWindow(parent)
    {
        R = 90.0;
        q = 24;
        p = 24;
    
        kol_toch = 180;
    }
    
    Krug_qt::~Krug_qt()
    {
    
    }
    
    void Krug_qt::fillVertexArray()
    {
        float grad = 360.0/kol_toch;
        float grad_kol_toch = 0.0;
        // градусы*M_PI/180 = радианы
        int j;
        for (j = 0; j <= kol_toch; ++j){
            integerVector.append(R * cos(grad_kol_toch*M_PI/180));
            integerVector.append(R * sin(grad_kol_toch*M_PI/180));
            grad_kol_toch = grad_kol_toch + grad;
        }
    }
    
    void Krug_qt::paintEvent(QPaintEvent *event)
    {
        integerVector.clear();
        QPainter painter(this);                                   // Создаём объект отрисовщика
        QPen pen_abris(Qt::black, 2, Qt::SolidLine, Qt::FlatCap); // кисть обрисовки (компаса)
        painter.setRenderHint(QPainter::Antialiasing);            // убираем резкие кубики
        painter.setPen(pen_abris);                                // Устанавливаем кисть обрисовки
        fillVertexArray();                                        // Набираем массив
        painter.translate(this->width()/2, this->height()/2);     // смещение отрисовки
    
        qDebug() << integerVector.size();
    
        for(int i =0; i<integerVector.size(); i++)
        {
            painter.drawPoint(integerVector[i],integerVector[i++]);
            i++;
        }
    }
    

    main.cpp

    #include "krug_qt.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Krug_qt w;
        w.show();
    
        return a.exec();
    }
    

    Screenshot_20211021_130350.png

    1 Reply Last reply
    0
    • timob256T Offline
      timob256T Offline
      timob256
      wrote on last edited by
      #5

      #include "krug_qt.h"

      Krug_qt::Krug_qt(QWidget *parent)
      : QMainWindow(parent)
      {
      R = 90.0;
      q = 24;
      p = 24;

      kol_toch = 180;
      

      }

      Krug_qt::~Krug_qt()
      {

      }

      void Krug_qt::fillVertexArray()
      {
      float grad = 360.0/kol_toch;
      float grad_kol_toch = 0.0;
      // градусыM_PI/180 = радианы
      int j;
      for (j = 0; j <= kol_toch; ++j){
      integerVector.append(R * cos(grad_kol_toch
      M_PI/180.0));
      integerVector.append(R * sin(grad_kol_toch*M_PI/180.0));
      grad_kol_toch = grad_kol_toch + grad;
      }
      }

      void Krug_qt::paintEvent(QPaintEvent *event)
      {
      integerVector.clear();
      QPainter painter(this); // Создаём объект отрисовщика
      QPen pen_abris(Qt::black, 2, Qt::SolidLine, Qt::FlatCap); // кисть обрисовки (компаса)
      painter.setRenderHint(QPainter::Antialiasing); // убираем резкие кубики
      painter.setPen(pen_abris); // Устанавливаем кисть обрисовки
      fillVertexArray(); // Набираем массив
      painter.translate(this->width()/2, this->height()/2); // смещение отрисовки

      qDebug() << integerVector.size();
      
      int f = 0;
      for(int i =0; i<integerVector.size()/2; i++)
      {
          i++; f++;
          painter.drawPoint(QPointF(integerVector[--i],integerVector[i]));
          qDebug() << "f :"<<f;
      

      // painter.drawPoint(QPointF(integerVector[i],integerVector[i++]));
      // i++;
      }
      }

      Screenshot_20211021_153501.png

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi

        yes I also think its due to INT cutting.

        you could use
        https://doc.qt.io/qt-5.15/qpainter.html#drawPoint

        and QPointF instead and also change your list to use that type.

        1 Reply Last reply
        0
        • timob256T Offline
          timob256T Offline
          timob256
          wrote on last edited by
          #3

          ----and QPointF instead and also change your list to use that type.

          I do not know how to do this

          mrjjM 1 Reply Last reply
          0
          • timob256T timob256

            ----and QPointF instead and also change your list to use that type.

            I do not know how to do this

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #4

            @timob256

            Hi
            just change the type for the definition "integerVector"

            I assume you have something like
            std::vector<int > integerVector;
            and change into
            std::vector<QPointF> integerVector;

            and for the
            integerVector.append
            will just be using a QPointF instead.

            ah missed it.
            QVector<float> integerVector;
            so that will be
            QVector<QPointF> integerVector;

            and maybe not call it integer vector but points or something like that ? :)

            1 Reply Last reply
            2
            • timob256T Offline
              timob256T Offline
              timob256
              wrote on last edited by
              #5

              #include "krug_qt.h"

              Krug_qt::Krug_qt(QWidget *parent)
              : QMainWindow(parent)
              {
              R = 90.0;
              q = 24;
              p = 24;

              kol_toch = 180;
              

              }

              Krug_qt::~Krug_qt()
              {

              }

              void Krug_qt::fillVertexArray()
              {
              float grad = 360.0/kol_toch;
              float grad_kol_toch = 0.0;
              // градусыM_PI/180 = радианы
              int j;
              for (j = 0; j <= kol_toch; ++j){
              integerVector.append(R * cos(grad_kol_toch
              M_PI/180.0));
              integerVector.append(R * sin(grad_kol_toch*M_PI/180.0));
              grad_kol_toch = grad_kol_toch + grad;
              }
              }

              void Krug_qt::paintEvent(QPaintEvent *event)
              {
              integerVector.clear();
              QPainter painter(this); // Создаём объект отрисовщика
              QPen pen_abris(Qt::black, 2, Qt::SolidLine, Qt::FlatCap); // кисть обрисовки (компаса)
              painter.setRenderHint(QPainter::Antialiasing); // убираем резкие кубики
              painter.setPen(pen_abris); // Устанавливаем кисть обрисовки
              fillVertexArray(); // Набираем массив
              painter.translate(this->width()/2, this->height()/2); // смещение отрисовки

              qDebug() << integerVector.size();
              
              int f = 0;
              for(int i =0; i<integerVector.size()/2; i++)
              {
                  i++; f++;
                  painter.drawPoint(QPointF(integerVector[--i],integerVector[i]));
                  qDebug() << "f :"<<f;
              

              // painter.drawPoint(QPointF(integerVector[i],integerVector[i++]));
              // i++;
              }
              }

              Screenshot_20211021_153501.png

              1 Reply Last reply
              0
              • timob256T Offline
                timob256T Offline
                timob256
                wrote on last edited by
                #6

                @ mrjj

                why can't I get a full circle ???

                #include "krug_qt.h"
                
                Krug_qt::Krug_qt(QWidget *parent)
                    : QMainWindow(parent)
                {
                    R = 90.0;
                    q = 24;
                    p = 24;
                
                    kol_toch = 180;
                }
                
                Krug_qt::~Krug_qt()
                {
                
                }
                
                void Krug_qt::fillVertexArray()
                {
                    float grad = 360.0/kol_toch;
                    float grad_kol_toch = 0.0;
                    // градусы*M_PI/180 = радианы
                    int j;
                    for (j = 0; j <= kol_toch; ++j){
                        integerVector.append(R * cos(grad_kol_toch*M_PI/180.0));
                        integerVector.append(R * sin(grad_kol_toch*M_PI/180.0));
                        grad_kol_toch = grad_kol_toch + grad;
                    }
                }
                
                void Krug_qt::paintEvent(QPaintEvent *event)
                {
                    integerVector.clear();
                    QPainter painter(this);                                   // Создаём объект отрисовщика
                    QPen pen_abris(Qt::black, 2, Qt::SolidLine, Qt::FlatCap); // кисть обрисовки (компаса)
                    painter.setRenderHint(QPainter::Antialiasing);            // убираем резкие кубики
                    painter.setPen(pen_abris);                                // Устанавливаем кисть обрисовки
                    fillVertexArray();                                        // Набираем массив
                    painter.translate(this->width()/2, this->height()/2);     // смещение отрисовки
                
                    qDebug() << integerVector.size();
                
                    int f = 0;
                    for(int i =0; i<integerVector.size()/2; i++)
                    {
                        i++; f++;
                        painter.drawPoint(QPointF(integerVector[--i],integerVector[i]));
                        qDebug() << "f :"<<f;
                
                //        painter.drawPoint(QPointF(integerVector[i],integerVector[i++]));
                //        i++;
                    }
                }
                

                Screenshot_20211021_153501.png

                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