使用qt编写一个程序,要求显示一个折线图,分成main.cpp mainwindow.h mainwindow.cpp三个文件,折线图的横坐标范围为0到1023,纵坐标范围为-50到50,要求图表可以随鼠标放大缩小和拉伸操作,请提供相应代码
时间: 2024-05-12 13:19:43 浏览: 85
Qt界面设计:利用QChart显示折线图(QT+VS C++)
5星 · 资源好评率100%
main.cpp:
```c++
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
```
mainwindow.h:
```c++
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
QT_CHARTS_USE_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QChartView *m_chartView;
QLineSeries *m_series;
qreal m_x;
qreal m_y;
void setupUI();
void setupChart();
void setupSeries();
void setupAxis();
protected:
void resizeEvent(QResizeEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp:
```c++
#include "mainwindow.h"
#include <QtCharts/QValueAxis>
#include <QMouseEvent>
#include <QScrollBar>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, m_chartView(new QChartView(this))
, m_series(new QLineSeries(this))
, m_x(0)
, m_y(0)
{
setupUI();
setupChart();
setupSeries();
setupAxis();
}
MainWindow::~MainWindow()
{
}
void MainWindow::setupUI()
{
setCentralWidget(m_chartView);
}
void MainWindow::setupChart()
{
QChart *chart = new QChart();
chart->addSeries(m_series);
chart->legend()->hide();
chart->setTitle("折线图");
m_chartView->setChart(chart);
m_chartView->setRenderHint(QPainter::Antialiasing);
m_chartView->setRubberBand(QChartView::RectangleRubberBand);
}
void MainWindow::setupSeries()
{
for (int i = 0; i < 1024; ++i) {
m_series->append(i, qSin(i / 50.0) * 20);
}
}
void MainWindow::setupAxis()
{
QValueAxis *axisX = new QValueAxis();
axisX->setRange(0, 1023);
axisX->setLabelFormat("%d");
axisX->setTitleText("横轴");
QValueAxis *axisY = new QValueAxis();
axisY->setRange(-50, 50);
axisY->setLabelFormat("%.2f");
axisY->setTitleText("纵轴");
QChart *chart = m_chartView->chart();
chart->setAxisX(axisX, m_series);
chart->setAxisY(axisY, m_series);
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
QMainWindow::resizeEvent(event);
QRectF rect = m_chartView->chart()->plotArea();
rect.setWidth(m_chartView->width());
m_chartView->chart()->setPlotArea(rect);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
m_x = event->pos().x();
m_y = event->pos().y();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
int dx = event->pos().x() - m_x;
int dy = event->pos().y() - m_y;
if (event->buttons() & Qt::LeftButton) {
m_chartView->chart()->scroll(dx, -dy);
}
m_x = event->pos().x();
m_y = event->pos().y();
}
void MainWindow::wheelEvent(QWheelEvent *event)
{
int delta = event->angleDelta().y();
qreal factor = qPow(1.0015, delta);
QRectF rect = m_chartView->chart()->plotArea();
qreal centerX = rect.x() + rect.width() / 2;
qreal centerY = rect.y() + rect.height() / 2;
qreal newWidth = rect.width() * factor;
qreal newHeight = rect.height() * factor;
if (newWidth > 10 && newHeight > 10) {
rect.setX(centerX - newWidth / 2);
rect.setY(centerY - newHeight / 2);
rect.setWidth(newWidth);
rect.setHeight(newHeight);
m_chartView->chart()->zoomIn(rect);
}
}
```
阅读全文