#include "myHMIDisplay.h" #include #include #include #include myHMIDisplay::myHMIDisplay(QWidget *parent) : QFrame(parent) { QTimer* timer = new QTimer(this); timer->start(50); connect(timer,&QTimer::timeout,this,&myHMIDisplay::OnTimerd); // stEquipment equipment1 ={ "ABC",50,50,10,10,10,{3} }; // m_equipments.append(equipment1); m_equipments.append({1,u8"熔制区",8,20,20,5,5,{0}}); m_equipments.append({3,u8"龙门",40,15,40,3,5,{3}}); m_equipments.append({3,u8"车",25,25,6,4,2,{3}}); m_equipments.append({3,u8"车",35,25,6,4,2,{3}}); m_equipments.append({3,u8"车",45,25,6,4,2,{3}}); m_equipments.append({3,u8"车",55,25,6,4,2,{3}}); m_equipments.append({5,u8"龙门1",70,20,10,5,1,{3}}); m_equipments.append({5,u8"龙门2",80,20,10,5,1,{3}}); m_equipments.append({5,u8"车1",70,30,5,5,1,{3}}); m_equipments.append({5,u8"车2",75,30,5,5,1,{3}}); m_equipments.append({5,u8"车3",80,30,5,5,1,{3}}); m_equipments.append({5,u8"车4",85,30,5,5,1,{3}}); m_equipments.append({1,u8"冷车间",8,50,10,5,5,{3}}); m_equipments.append({7,u8"刷锅",20,50,10,5,5,{255}}); m_equipments.append({7,u8"磨锅",35,50,10,5,5,{3}}); m_equipments.append({7,u8"切割1",50,45,10,3,5,{3}}); m_equipments.append({7,u8"切割2",50,55,10,3,5,{3}}); m_equipments.append({7,u8"测量",65,50,10,5,5,{3}}); m_equipments.append({7,u8"酸洗",80,50,10,5,5,{3}}); m_equipments.append({1,u8"净化车间",8,80,20,5,5,{3}}); m_equipments.append({7,u8"烘烤",20,80,10,5,5,{3}}); m_equipments.append({7,u8"人工目检",40,80,10,5,5,{3}}); m_equipments.append({7,u8"8条直传",60,80,10,5,5,{3}}); m_equipments.append({7,u8"拆包",80,80,10,5,5,{3}}); } void myHMIDisplay::OnTimerd() { nCount++; update(); } void myHMIDisplay::paintEvent(QPaintEvent *event) { //控件的绘制工具,通过该工具我们可以进行常用的绘制了 QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); //取得画布大小和中心方形。 int w = this->rect().width(); int h = this->rect().height(); int s; int wmargin=0; int hmargin=0; if (w>h) { s= h; wmargin=(w-s)/2; }else{ s= w; hmargin=(h-s)/2; } //画边框 QPen pen0(QColor(0x4D9CF8)); painter.setPen(pen0); painter.drawRoundRect(this->rect(),5,5); int thick = s/20; //线宽 QPen pen1(QColor(80,80,80)); //绘制弧线的背景 pen1.setWidth(thick); pen1.setCapStyle(Qt::RoundCap); // pen.setColor(QColor("#4D9CF8")); painter.setPen(pen1); QRectF in_rectangle(wmargin+10, hmargin+s/4, s-20, s-20); int in_startAngle = (0) * 16; int in_spanAngle = (180)*16; painter.drawArc(in_rectangle,in_startAngle,in_spanAngle); QPen pen2(lineColor); QBrush brush1(QColor(0xbb,0xbb,0x66,0xff)); QBrush brush2(QColor(0x88,0x88,0x66,0xff)); QBrush brush3(QColor(0x88,0x88,0x66,0xff)); QBrush RunningBrush(RunningColor); QBrush StopedBrush(StopedColor); QBrush WarningBrush(WarningColor); QPen pen3(nameColor); //绘制弧线的背景 // pen2.setWidth(thick); // pen1.setCapStyle(Qt::RoundCap); // pen.setColor(QColor("#4D9CF8")); painter.setPen(pen2); // painter.setBrush(brush1); int n=m_equipments.count(); for (int i=0;irect(),s1); painter.drawText(rect2,Qt::AlignCenter,sName); } if (thetype & eqTypeIndicator ){ //draw indicator 指示器 painter.setBrush(brush3); QRect rect3(ex-el/4,ey-ew*7/8,el/2,ew/4); painter.fillRect(rect3,brush1); painter.drawRect(rect3); QRect rect31(ex-el/4,ey-ew*7/8,el/8,ew/4); if (theequipment.bRunning){ painter.fillRect(rect31,RunningBrush); } // painter.drawRect(rect31); QRect rect32(ex-el/16,ey-ew*7/8,el/8,ew/4); if (theequipment.bStoped){ //painter.fillRect(rect32,StopedBrush); painter.setBrush(StopedBrush); } painter.drawRect(rect32); QRect rect33(ex+2*el/16,ey-ew*7/8,el/8,ew/4); if (theequipment.bWarning){ painter.setBrush(WarningBrush); painter.fillRect(rect33,WarningBrush); } painter.drawRect(rect33); } } // in_startAngle = (nOffset + 90+270) * 16; // in_spanAngle = (45)*16; // painter.drawArc(in_rectangle,in_startAngle,in_spanAngle); return; // QRect rect2(wmargin + 20, hmargin +20, s-40,s-40); QString s1; /* //添加绘制的字体和字号 s1=QString::number(int(position)); int l = s1.length(); double fr = sqrt(l*l + 4); painter.setFont(QFont("Microsoft YaHei",(s-20)/fr)); // painter.drawText(this->rect(),s1); painter.drawText(rect2,Qt::AlignCenter,s1); */ } void myHMIDisplay::SetRange(int n) { range=n; } void myHMIDisplay::SetPosition(float n) { position=n; update(); } void myHMIDisplay::SetTitle(QString title) { sTitle = title; } void myHMIDisplay::SetUnit(QString unit) { sUnit = unit; } void myHMIDisplay::SetColor(QColor Start,QColor End) { color1 = Start; color2 = End; } void drawArc() { int nCount=0; int position; int range=0; int thick=2; int wmargin=10; int hmargin=10; int s=2; QColor color1,color2; QPainter painter; QString sTitle="1122"; QString sUnit; int nOffset = nCount*5 % 180; position = nOffset *range /180; QPen pen2(QColor(0x4D9CF8)); pen2.setWidth(thick); pen2.setCapStyle(Qt::RoundCap); QRectF sec_rectangle(wmargin+10, hmargin+s/4, s-20, s-20); int sec_startAngle = 180 * 16; int sec_spanAngle = -1*((position / range)*180)*16; QConicalGradient gradient; gradient.setCenter(sec_rectangle.center()); gradient.setAngle(190); gradient.setColorAt(0, color2); gradient.setColorAt(0.5, color2); gradient.setColorAt(1, color1); // int arcLengthApproximation = m_width + m_width / 3; QPen pen3(QBrush(gradient), thick); pen3.setCapStyle(Qt::RoundCap); painter.setPen(pen3); painter.drawArc(sec_rectangle,sec_startAngle,sec_spanAngle); //绘制文本,绘制白色的文字 painter.setPen(QPen(QColor(255,255,255))); QString s1; s1=sTitle; //显示标题 QRect rect2(wmargin + 20, hmargin +10, s-40,s/8); painter.setFont(QFont("Microsoft YaHei",s/12)); // painter.drawText(this->rect(),s1); painter.drawText(rect2,Qt::AlignCenter,s1); //显示前后刻度 painter.setFont(QFont("Microsoft YaHei",s/18)); s1="0"; // painter.drawText(this->rect(),s1); rect2.setRect(wmargin,hmargin+s*3/4,20,s/16); painter.drawText(rect2,Qt::AlignCenter,s1); s1=QString::number(range); // painter.drawText(this->rect(),s1); rect2.setRect(wmargin+s-30,hmargin+s*3/4,40,s/16); painter.drawText(rect2,Qt::AlignCenter,s1); //显示当前数量 painter.setFont(QFont("Microsoft YaHei",s/8)); s1=QString::number(position); // painter.drawText(this->rect(),s1); rect2.setRect(wmargin+10,hmargin+10-s/20,s-20,s-20); painter.drawText(rect2,Qt::AlignCenter,s1); //显示单位 painter.setFont(QFont("Microsoft YaHei",s/16)); s1=sUnit; // painter.drawText(this->rect(),s1); rect2.setRect(wmargin+10,hmargin+10+s/10,s-20,s-20); painter.drawText(rect2,Qt::AlignCenter,s1); }