How to draw with vertices, circle (and other shapes) ?
-
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(); }
-
#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_tochM_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++;
}
} -
Hi
yes I also think its due to INT cutting.
you could use
https://doc.qt.io/qt-5.15/qpainter.html#drawPointand QPointF instead and also change your list to use that type.
-
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 ? :)
-
#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_tochM_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++;
}
} -
@ 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++; } }