正弦曲线绘制QT代码
时间: 2024-06-20 19:02:33 浏览: 11
在使用Qt库绘制正弦曲线时,你可以使用QPainter和QPointF等类来创建图形。以下是一个简单的示例代码,它会创建一个窗口并绘制一条正弦波:
```cpp
#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <QPointF>
#include <cmath>
class MyWindow : public QWidget
{
Q_OBJECT
public:
MyWindow(QWidget *parent = nullptr) : QWidget(parent)
{
setMinimumSize(600, 400);
setWindowTitle("Sinusoidal Curve");
}
protected:
void paintEvent(QPaintEvent *)
{
QPainter painter(this);
// 设置颜色
painter.setPen(QColor(0, 0, 255)); // blue color
// 设置坐标轴
painter.drawLine(0, 0, width(), 0);
painter.drawLine(0, height(), 0, 0);
// 绘制正弦波
const int sampleCount = width();
for (int x = 0; x <= sampleCount; ++x)
{
double angleInRadians = static_cast<double>(x) / sampleCount * 2 * M_PI;
double y = sin(angleInRadians) * height() / 2 + height() / 2;
QPointF point(x, y);
painter.drawPoint(point);
}
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWindow window;
window.show();
return app.exec();
}
```
在这个例子中,我们首先创建了一个名为MyWindow的窗口,然后在`paintEvent`方法中实现了绘画。我们计算了从0到窗口宽度的点的坐标,根据这些点的x值计算对应的正弦函数值y,并在画布上绘制出这些点。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)