C++Qt5.14.2版本不用UI形式完整代码绘制出频率响应曲线图和幅度响应曲线图
时间: 2024-03-07 20:49:47 浏览: 40
好的,下面是一个C++和Qt5.14.2版本的示例代码,它使用Qt Charts模块绘制频率响应曲线图和幅度响应曲线图:
```c++
#include <QApplication>
#include <QtCharts>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Create a chart
QChart *chart = new QChart();
// Set the title
chart->setTitle("Frequency and Amplitude Response");
// Create X-axis
QValueAxis *axisX = new QValueAxis;
axisX->setTitleText("Frequency (Hz)");
// Create Y-axis for frequency response
QValueAxis *axisY1 = new QValueAxis;
axisY1->setTitleText("Frequency Response");
// Create Y-axis for amplitude response
QValueAxis *axisY2 = new QValueAxis;
axisY2->setTitleText("Amplitude Response");
// Add the axes to the chart
chart->addAxis(axisX, Qt::AlignBottom);
chart->addAxis(axisY1, Qt::AlignLeft);
chart->addAxis(axisY2, Qt::AlignRight);
// Create a series for the frequency response curve
QLineSeries *freqSeries = new QLineSeries();
// Add data to the series
freqSeries->append(0, 0);
freqSeries->append(1000, 0.5);
freqSeries->append(2000, 1);
freqSeries->append(3000, 0.8);
freqSeries->append(4000, 0.6);
freqSeries->append(5000, 0.4);
// Create a series for the amplitude response curve
QLineSeries *ampSeries = new QLineSeries();
// Add data to the series
ampSeries->append(0, 1);
ampSeries->append(1000, 0.8);
ampSeries->append(2000, 0.5);
ampSeries->append(3000, 0.3);
ampSeries->append(4000, 0.2);
ampSeries->append(5000, 0.1);
// Add the series to the chart
chart->addSeries(freqSeries);
chart->addSeries(ampSeries);
// Attach the series to the axes
chart->setAxisX(axisX, freqSeries);
chart->setAxisY(axisY1, freqSeries);
chart->setAxisY(axisY2, ampSeries);
// Create a view and set the chart as its model
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
// Show the chart view
chartView->show();
return a.exec();
}
```
这段代码创建了一个简单的频率响应曲线图和幅度响应曲线图,您可以根据自己的需要修改数据来绘制不同的曲线。要绘制幅度响应曲线图,您需要创建一个新的QLineSeries并将其添加到图表中,然后将其附加到一个新的Y轴上。最后,使用setAxisY()函数将相应的系列与它们的Y轴连接起来。
阅读全文