关于 c :Qpainter 情节消失

Qpainter plot disappears

我使用 Qt 框架创建了一个应用程序,并使用 QPainter 绘制了一些图形。

我可以画出exp函数,我想画出y'=y微分方程(欧拉法)的数值解,也就是exp(x),看起来画得对,但是当我尝试调整大小时在窗口中,exp 函数仍然存在,但由于某种原因近似值消失了。

可能是什么问题?

(我知道代码很丑,因为现在我把所有东西都放在了标题中)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QPointF>
#include <math.h>
#include <QWidget>
#include <QPainter>
#include <QVector>
#include <cmath>
#include <QImage>
#include <Qt>
#include <QPen>

class Plot : public QWidget
{
public:
 Plot() {}
 void init()
 {
     QPainter painter(this);
     QPen my_pen;
     my_pen.setStyle(Qt::SolidLine);
     my_pen.setWidth(5);
     my_pen.setBrush(Qt::blue);
     my_pen.setColor(Qt::blue);
     my_pen.setCapStyle(Qt::RoundCap);
     my_pen.setJoinStyle(Qt::RoundJoin);
     painter.setPen(my_pen);
     painter.setRenderHint(QPainter::Antialiasing);
     painter.translate(width()/2.0f,height()/2.0f);
     painter.scale(1/10.0f,-1/10.0f);

     //DRAWING THE COORDINATE SYSTEM
     painter.drawLine(QPointF(-m_width/2.0f,0.0f),QPointF(m_width/2.0f,0.0f));
     painter.drawLine(QPointF(0.0f,-m_height/2.0f),QPointF(0.0f,m_height/2.0f));

     //SPLITTING Y AND X ASIS TO UNITS
     for(int i = -20/2.0f; i <= 20/2.0f; ++i)
     {
         painter.drawLine(QPointF(i*m_unit,-10),QPointF(i*m_unit,10));
     }

     for(int i = -20/2.0f; i <= 20/2.0f; ++i)
     {
         painter.drawLine(QPointF(-10,i*m_unit),QPointF(10,i*m_unit));
     }

     //DRAWING EXP(X) FUNCTION
     for(int i = -m_number_of_points/2; i < m_number_of_points/2; ++i)
     {
         m_point_array[i+m_number_of_points/2] = QPointF(i,m_unit*exp(i/m_unit));
     }

     for(int i = 0; i < m_number_of_points; ++i)
     {
         painter.drawPoint(m_point_array[i]);
     }

     //DRAWING THE NUMERICAL APPROXIMATION OF THE SOLUTION OF y'=y DIFF. EQ, WHICH IS exp(x)
     for(int i = 0; i < m_number_of_basepoints; ++i)
     {
         m_approx_point_array[i] = QPointF(m_unit*((i+1)*m_step+m_first_x_coord),m_unit*(m_initial_condition + m_step*derived_func(m_initial_condition)));
         m_initial_condition = m_initial_condition + m_step*derived_func(m_initial_condition);
     }
     my_pen.setColor(Qt::red);
     painter.setPen(my_pen);
     painter.drawLine(QPointF(m_unit*m_first_x_coord,m_unit*m_xxx),QPointF(m_approx_point_array[0]));
     for(int i = 0; i < m_number_of_basepoints-1; ++i)
     {
         painter.drawLine(m_approx_point_array[i],m_approx_point_array[i+1]);
     }
 }

 void paintEvent(QPaintEvent*)
 {
     init();
 }


 float derived_func(float param)
 {
     return param;
 }

  //INITIALIZING THE VALUES FOR COORDINATE SYSTEM
 float m_ratio_x = 1/10.0f;
 float m_ratio_y = -1/10.0f;
 float m_width = width() * (1.0f/m_ratio_x);
 float m_height = height() * (1.0f/m_ratio_y);
 float m_unit = 300.0f;

  //INITIALIZING THE VALUES FOR DRAWING exp(x)
 int m_number_of_points = m_width;
 QPointF* m_point_array = new QPointF[m_number_of_points];

 //INITIALIZING THE VALUES FOR APPROXIMATION
 int m_number_of_basepoints = 1000;
 QPointF* m_approx_point_array = new QPointF[m_number_of_basepoints];
 float m_first_x_coord = -5.0f;
 float m_initial_condition = 1/exp(5);
 float m_last_x_coord = 2.0f;
 float m_step = (m_last_x_coord - m_first_x_coord)/m_number_of_basepoints;
 float m_xxx = m_initial_condition;
};

#endif

不能确定,但??我想这很可能是由于每次计算时都修改了 m_initial_condition 变量。作为诊断此类问题的好方法,我建议将 const 添加到实际上不应更改的变量中。