⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bar.cpp

📁 一个基于QT的电网监控程序
💻 CPP
字号:
#include "bar.h" #include <qvariant.h>#include <qtooltip.h>#include <qwhatsthis.h>#include <qpainter.h>#include <qframe.h>#include <stdlib.h>#include <stdio.h>Bar::Bar(QWidget *parent,const char *name)	:QFrame(parent,name){	init();	if ( !name )		setName( "bar" );    	resize( 500, 200 ); 		TextLabel2 = new QLabel( this, "TextLabel2" );    	TextLabel2->setGeometry( QRect( 35, BASELINE+3, 16, 16 ) );     	TextLabel2->setText( tr( "1" ) );    	TextLabel3 = new QLabel( this, "TextLabel3" );    	TextLabel3->setGeometry( QRect( 92, BASELINE+3, 16, 16 ) );     	TextLabel3->setText( tr( "5" ) );    	TextLabel4 = new QLabel( this, "TextLabel4" );    	TextLabel4->setGeometry( QRect( 147, BASELINE+3, 16, 16 ) );     	TextLabel4->setText( tr( "9" ) );    	TextLabel5 = new QLabel( this, "TextLabel5" );    	TextLabel5->setGeometry( QRect( 199, BASELINE+3, 16, 16 ) );     	TextLabel5->setText( tr( "13" ) );    	TextLabel6 = new QLabel( this, "TextLabel6" );    	TextLabel6->setGeometry( QRect( 255, BASELINE+3, 16, 16 ) );     	TextLabel6->setText( tr( "17" ) );    	TextLabel7 = new QLabel( this, "TextLabel7" );    	TextLabel7->setGeometry( QRect( 312, BASELINE+3, 16, 16 ) );     	TextLabel7->setText( tr( "21" ) );    	TextLabel8 = new QLabel( this, "TextLabel8" );    	TextLabel8->setGeometry( QRect( 368, BASELINE+3, 16, 16 ) );     	TextLabel8->setText( tr( "25" ) );		TextLabel9 = new QLabel( this, "TextLabel9" );    	TextLabel9->setGeometry( QRect( 425, BASELINE+3, 16, 16 ) );     	TextLabel9->setText( tr( "29" ) );		TextLabel15 = new QLabel( this, "TextLabel15" );    	TextLabel15->setGeometry( QRect( 475, BASELINE-GRID-10, 24, 16 ) );     	TextLabel15->setText( tr( "10%" ) );    	TextLabel10 = new QLabel( this, "TextLabel10" );    	TextLabel10->setGeometry( QRect( 475, BASELINE-GRID*2-10, 24, 16 ) );     	TextLabel10->setText( tr( "20%" ) );    	TextLabel11 = new QLabel( this, "TextLabel11" );     	TextLabel11->setGeometry( QRect( 475, BASELINE-GRID*4-10, 24, 16 ) );     	TextLabel11->setText( tr( "40%" ) );    	TextLabel12 = new QLabel( this, "TextLabel12" );    	TextLabel12->setGeometry( QRect( 475, BASELINE-GRID*6-10, 24, 16 ) );     	TextLabel12->setText( tr( "60%" ) );    	TextLabel13 = new QLabel( this, "TextLabel13" );    	TextLabel13->setGeometry( QRect( 475, BASELINE-GRID*8-10, 24, 16 ) );     	TextLabel13->setText( tr( "80%" ) );	    	TextLabel16 = new QLabel( this, "TextLabel16" );    	TextLabel16->setGeometry( QRect( 475, BASELINE-GRID*9-10, 24, 16 ) );     	TextLabel16->setText( tr( "90%" ) );	    	TextLabel14 = new QLabel( this, "TextLabel14" );    	TextLabel14->setGeometry( QRect( 0, 30, 16, 16 ) );     	TextLabel14->setText( tr( "L1" ) );    	TextLabel1 = new QLabel( this, "TextLabel1" );    	TextLabel1->setGeometry( QRect( 0, 60, 12, 20 ) );     	TextLabel1->setText( tr( "I" ) );		TextLabel17=new QLabel(this,"TextLabel17");	TextLabel17->setGeometry(QRect( 180, 0, 150, 17 ));	TextLabel17->setAlignment(AlignHCenter);//	TextLabel17->setText( tr( "Voltage Harmonic" ) );		pal=new QPalette();	cg=new QColorGroup();}Bar::~Bar(){	delete TextLabel1;	delete TextLabel2;	delete TextLabel3;	delete TextLabel4;	delete TextLabel5;		delete TextLabel6;	delete TextLabel7;	delete TextLabel8;	delete TextLabel9;	delete TextLabel10;		delete TextLabel11;	delete TextLabel12;	delete TextLabel13;	delete TextLabel14;	delete TextLabel15;	delete TextLabel16;	delete TextLabel17;	delete pal;	delete cg;}void Bar::init(){	int i=0;	setFrameStyle(QFrame::Box | QFrame::Raised);		for(i=0;i<64;i++)	 per[i]=0;}void Bar::setLineString(const QString &Line){	TextLabel14->setText( Line );}void Bar::setAVString(const QString &AV){	TextLabel1->setText(AV);}void Bar::setBarColor(const QColor &color){	this->BarColor=color;}void Bar::setTitle(const QString &title,const QColor &color){	cg->setColor( QColorGroup::Foreground, color );    	cg->setColor( QColorGroup::Button, QColor( 230, 230, 230) );    	cg->setColor( QColorGroup::Light, white );    	cg->setColor( QColorGroup::Midlight, QColor( 242, 242, 242) );    	cg->setColor( QColorGroup::Dark, QColor( 115, 115, 115) );    	cg->setColor( QColorGroup::Mid, QColor( 153, 153, 153) );    	cg->setColor( QColorGroup::Text, black );    	cg->setColor( QColorGroup::BrightText, QColor( 250, 250, 250) );    	cg->setColor( QColorGroup::ButtonText, black );    	cg->setColor( QColorGroup::Base, white );    	cg->setColor( QColorGroup::Background, QColor( 193, 193, 193) );    	cg->setColor( QColorGroup::Shadow, black );    	cg->setColor( QColorGroup::Highlight, QColor( 76, 89, 166) );    	cg->setColor( QColorGroup::HighlightedText, white );    	pal->setActive( *cg );/*	cg->setColor( QColorGroup::Foreground, QColor( 0, 0, 255) );    	cg->setColor( QColorGroup::Button, QColor( 230, 230, 230) );    	cg->setColor( QColorGroup::Light, white );    	cg->setColor( QColorGroup::Midlight, white );    	cg->setColor( QColorGroup::Dark, QColor( 115, 115, 115) );    	cg->setColor( QColorGroup::Mid, QColor( 153, 153, 153) );    	cg->setColor( QColorGroup::Text, black );    	cg->setColor( QColorGroup::BrightText, QColor( 250, 250, 250) );    	cg->setColor( QColorGroup::ButtonText, black );    	cg->setColor( QColorGroup::Base, white );    	cg->setColor( QColorGroup::Background, QColor( 230, 230, 230) );    	cg->setColor( QColorGroup::Shadow, black );    	cg->setColor( QColorGroup::Highlight, QColor( 76, 89, 166) );    	cg->setColor( QColorGroup::HighlightedText, white );    	pal->setInactive( *cg );	cg->setColor( QColorGroup::Foreground, QColor( 128, 128, 128) );    	cg->setColor( QColorGroup::Button, QColor( 230, 230, 230) );    	cg->setColor( QColorGroup::Light, white );    	cg->setColor( QColorGroup::Midlight, white );    	cg->setColor( QColorGroup::Dark, QColor( 115, 115, 115) );    	cg->setColor( QColorGroup::Mid, QColor( 153, 153, 153) );    	cg->setColor( QColorGroup::Text, black );    	cg->setColor( QColorGroup::BrightText, QColor( 250, 250, 250) );    	cg->setColor( QColorGroup::ButtonText, QColor( 128, 128, 128) );    	cg->setColor( QColorGroup::Base, white );    	cg->setColor( QColorGroup::Background, QColor( 230, 230, 230) );    	cg->setColor( QColorGroup::Shadow, black );    	cg->setColor( QColorGroup::Highlight, QColor( 76, 89, 166) );    	cg->setColor( QColorGroup::HighlightedText, white );	pal->setDisabled( *cg );*/	TextLabel17->setPalette( *pal );	TextLabel17->setText( title);}void Bar::paintEvent(QPaintEvent*){   	int i,tmp1;   	int xPos;	   	painter=new QPainter;     	painter->begin(this);   //   	painter->lineTo(240,180);	painter->setPen(QPen(gray,1,QPen::DashLine));   	painter->drawLine(20,BASELINE-GRID,473,BASELINE-GRID);		//%10线   	painter->drawLine(20,BASELINE-GRID*2,473,BASELINE-GRID*2);		//%20线   	painter->drawLine(20,BASELINE-GRID*4,473,BASELINE-GRID*4);		//%40线   	painter->drawLine(20,BASELINE-GRID*6,473,BASELINE-GRID*6);		//%60线   	painter->drawLine(20,BASELINE-GRID*8,473,BASELINE-GRID*8);		//%80线   	painter->drawLine(20,BASELINE-GRID*9,473,BASELINE-GRID*9);		//%90线		painter->setPen(QPen(black,1,QPen::SolidLine));   	painter->drawLine(18,BASELINE+2,473,BASELINE+2);	   	painter->drawLine(18,STARTLINE,18,BASELINE+2);   	painter->setBrush(QBrush(this->BarColor,SolidPattern));      	xPos=20;   	for(i=0;i<32;i++){//     		painter->drawRect(20,160-150,3,150);//     		tmp=rand()%160;		tmp1=per[i];     		painter->drawRect(xPos,BASELINE-tmp1,11,tmp1);    	 	xPos+=14;   	}    	painter->end();		delete painter;}void Bar::drawBar(double *Harmonic,int cnt){	double tmp,tmp1;	int i;	//	this->erase();					// erase widget contents	tmp=0;	for(i=0;i<cnt/2;i++){		tmp+=Harmonic[i];	}	if(tmp==0)	 return;	tmp1=tmp;	for(i=0;i<cnt/2;i++){		this->per[i]=(int)((BASELINE-STARTLINE)*Harmonic[i]/tmp1);//		qDebug("%d=%d",i,per[i]);	}	this->update();					// draw bar	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -