功能描述:绘制曲线和散点图,添加图例信息,可以进行缩放、移动,鼠标在曲线上时显示当前坐标点
QChart功能类
-
继承QGraphicsView
-
重写鼠标事件函数
protected:void resizeEvent(QResizeEvent *event);void mouseMoveEvent(QMouseEvent *event);void mousePressEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void wheelEvent(QWheelEvent *event);
-
设置QChart对像基本属性
QFont f;f.setFamily("Times New Roman");f.setBold(true);f.setPointSize(12);// 创建图表m_chart = new QChart();m_chart->legend()->hide();m_chart->setTheme(QChart::ChartThemeBlueIcy);m_chart->setDropShadowEnabled(true);m_chart->setAutoFillBackground(true);m_chart->createDefaultAxes();m_chart->setAnimationOptions(QChart::SeriesAnimations);m_chart->setMargins(QMargins(0, 0, 0, 0));m_chart->setTitle("曲线");m_chart->setTitleFont(f);m_chart->legend()->setVisible(true);m_chart->legend()->detachFromChart();m_chart->legend()->setBackgroundVisible(true);m_chart->legend()->setBrush(QBrush(QColor(128, 128, 128, 80)));m_chart->legend()->setPen(QPen(QColor(192, 192, 192, 192)));QFont font = m_chart->legend()->font();font.setPointSizeF(6);m_chart->legend()->setFont(font);mAxisX = new QValueAxis();mAxisY = new QValueAxis();QFont font_axis;font_axis.setFamily("Times New Roman");font_axis.setBold(true);mAxisX->setRange(0, 0.1);mAxisY->setRange(0, 40);mAxisX->setLinePenColor(Qt::black);mAxisY->setLinePenColor(Qt::black);mAxisX->setLabelFormat("%.1e");mAxisY->setLabelFormat("%.1e");//mAxisX->setGridLineVisible(false);//mAxisY->setGridLineVisible(false);mAxisX->setTitleText("X轴");mAxisY->setTitleText("Y轴");mAxisX->setTitleFont(font_axis);mAxisY->setTitleFont(font_axis);setDragMode(QGraphicsView::NoDrag);setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);setRenderHint(QPainter::Antialiasing);setContentsMargins(QMargins(0, 0, 0, 0));scene()->addItem(m_chart);m_chart->addAxis(mAxisX, Qt::AlignBottom);m_chart->addAxis(mAxisY, Qt::AlignLeft);m_chart->setAcceptHoverEvents(true);// 设置图例字体大小QFont legendFont = m_chart->legend()->font();legendFont.setPointSize(8);legendFont.setBold(true);m_chart->