void MainWindow::initPainter() {
this->pImg = new QPixmap(imgWidth, imgHeight);
this->pPainter = new QPainter(pImg); //plmg指定绘图设备
pPainter->eraseRect(0, 0, pImg->width(), pImg->height());
pPainter->setRenderHint(QPainter::Antialiasing); //抗锯齿
QPen myPen; //画笔颜色设置
myPen.setColor(Qt::black); //笔颜色设置为黑色
myPen.setWidth(2);
pPainter->setPen(myPen);
//坐标转换
pPainter->translate(5, pImg->height()/2.0); //原点转化为纵坐标中点
QFont font; //字体设置
font.setPixelSize(10); //像素
font.setBold(true); //像素
pPainter->setFont(font);
}
//==========画坐标轴函数==========
void MainWindow::drawAxis() {
pPainter->eraseRect(-5, -pImg->height()/2.0, pImg->width(),pImg->height());
//横坐标 每隔5*scale画一条刻度
pPainter->drawLine(0, 0, pImg->width()-5, 0);
for(int i=0, pos=scale*5; pos< pImg->width(); i++,pos=i*scale*5) {
pPainter->drawLine(pos, -5, pos, 0);
if(0==i%2) {
pPainter->drawText(pos-3, 10, QString::number(i*5,10));
}
}
//纵坐标 每隔5*scale画一条刻度
pPainter->drawLine(0,-pImg->height()/2,0,pImg->height()/2);
for(int i=1,pos=scale*5;pos<pImg->height()/2;i++,pos=i*scale*5)
{
pPainter->drawLine(0,pos,5,pos);
pPainter->drawLine(0,-pos,5,-pos);
pPainter->drawText(8,-pos,QString::number(i*5,10));
pPainter->drawText(8,pos,QString::number(-i*5,10));
}
}
int MainWindow::drawSigTime(int x[],double y[],int n) {
QPointF pointF[imgWidth]; //目前最大的像素数
if(n>imgWidth/scale) return -1;
for (int i=0;i<n;++i){
pointF[i].setX(x[i]*scale);
pointF[i].setY(-y[i]*scale);// 坐标反转并缩放
}
QPainterPath path; // 使用的平滑曲线的坐标创建 QPainterPath
path.moveTo(pointF[0]);
for (int i = 1; i < n; ++i) path.lineTo(pointF[i]);
pPainter->drawPath(path);
return 0;
}