样式图:
.ui
在sloem1.ui文件中,拖入一个label控件,
头文件.h
#include "QtGui/QPainter.h"
#include "QtCore/QTimer.h"
protected:
bool eventFilter(QObject *obj,QEvent *event);
void labelPaint();
public slots:
//动态曲线
void drawCoordinateSystem(QPainter *painter);
void drawDynamicSpline(QPainter *painter);
void drawDynamicPiont();//动态点
源文件.cpp
static int count = 60;//统一改变,动态曲线所有参数
static int i = 0;//动态曲线点数,所要调用的参数
static int num = 99;//动态曲线点数(不同要求可修改)
static double dynamicPoint[99];//存储动态曲线点数的数组,移动时需要存储以前的数据,(不同要求需修改)
static double interval = count * 12 * 1.0 /( num-1 );//每一个点之间的间隔
sloem1::sloem1(QWidget* pParent, const QString& rszName)
: SlGfwDialogForm(pParent, rszName)
{
setupUi(this);
this->label->installEventFilter(this->label);
QTimer * timer = new QTimer(this);
QObject::connect(timer,
SIGNAL(timeout()),
this,
SLOT(update()));
timer->start(1000);//毫秒
} // <-- sloem1::sloem1 ()
//事件过滤器
bool sloem1::eventFilter(QObject *watched, QEvent *event)
{
if(watched == this->label && event->type() == QEvent::Paint)
{
labelPaint();
}
//交由QWidget的事件过滤器处理
return QWidget::eventFilter(watched,event);
}
//==============================动态曲线图============================
void sloem1::labelPaint()
{
int width = this -> width ();//标准ui宽度559
int height = this -> height();//标准ui高度394
int side = qMin( width, height ) / 2;
QPainter painter( this->label );
painter.setRenderHints ( QPainter::Antialiasing | QPainter::TextAntialiasing );//抗锯齿
painter.translate ( 165, 60 );//图形中心点,默认中心点左上角
painter.scale (side / 520.0, side / 520.0);//图形大小
//动态曲线
drawCoordinateSystem(&painter);
drawDynamicPiont();
drawDynamicSpline(&painter);
}
void sloem1::drawCoordinateSystem(QPainter *painter)
{
//int count = 60;//局部变量,统一改变所有参数
painter->save();
painter->setPen(Qt::black);
painter->drawLine(QPointF(-count*6,count*3),QPointF( count*6, count*3 ));//x轴
painter->drawLine(QPointF(-count*6,count*3),QPointF(-count*6,-count*2.5));//y轴
painter->setPen(Qt::DashLine);//虚线
painter->drawLine(QPointF(-count*6, count*2),QPointF(count*6, count*2));
painter->drawLine(QPointF(-count*6, count*1),QPointF(count*6, count*1));
painter->drawLine(QPointF(-count*6, count*0),QPointF(count*6, count*0));
painter->drawLine(QPointF(-count*6,-count*1),QPointF(count*6,-count*1));
painter->drawLine(QPointF(-count*6,-count*2),QPointF(count*6,-count*2));
QFont font;//字体
font.setPixelSize(40);
painter->setFont(font);
painter->drawText(-count*7.2, count*2.2,"1%" );
painter->drawText(-count*7.2, count*1.2,"2%");
painter->drawText(-count*7.2, count*0.2,"3%");
painter->drawText(-count*7.2,-count*0.8,"4%");
painter->drawText(-count*7.2,-count*1.8,"5%");
painter->restore();
}
void sloem1::drawDynamicPiont()
{
if( i < num )//坐标轴上点没有满时,
{
dynamicPoint[i] = qrand()%180;
}
i = i + 1;
if( i == num + 1 )//坐标轴上点已满,全体向左移动
{
i = i - 2;//坐标轴最后一个点
for( int j = 0; j < num - 1; j++ )
{
dynamicPoint[j] = dynamicPoint[j+1];
}
dynamicPoint[i] = qrand()%180;
i = i + 1;//不再进入坐标轴没满的循环
}
}
void sloem1::drawDynamicSpline(QPainter *painter)
{
painter->save();
QPen pen;
pen.setColor(Qt::white);
pen.setWidth(10);
painter->setPen(pen);
//painter->drawPoint(QPointF(-count*4,count*3));//坐标原点
;
//把横坐标按照点数分配;count*8/num,总长度/点数
for( int j = 0; j < i; j++ )
{
painter->drawPoint(QPointF(interval*j-interval*((num-1)/2),dynamicPoint[j]));//
}
pen.setColor(QColor(255,192,203));
pen.setWidth(4);
painter->setPen(pen);
//把点连成折线
if( i != 0 )
{
for( int k = 0; k < i-1; k++ )
{
painter->drawLine(QPointF(interval*k-interval*((num-1)/2),dynamicPoint[k]),
QPointF(QPointF(interval*(k+1)-interval*((num-1)/2),dynamicPoint[k+1])));
}
}
painter->restore();
}