Qt绘制指南针(仪表盘绘制封装使用示例)
立即下载
资源介绍:
本示例是在Qt中绘制一个指南针,通过继承QWidget类,并重写其paintEvent函数来实现。并对仪表盘绘制进行封装。
#include "qcgaugewidget.h"
QcGaugeWidget::QcGaugeWidget(QWidget *parent) :
QWidget(parent)
{
setMinimumSize(250,250);
}
QcBackgroundItem *QcGaugeWidget::addBackground(float position)
{
QcBackgroundItem * item = new QcBackgroundItem(this);
item->setPosition(position);
mItems.append(item);
return item;
}
QcDegreesItem *QcGaugeWidget::addDegrees(float position)
{
QcDegreesItem * item = new QcDegreesItem(this);
item->setPosition(position);
mItems.append(item);
return item;
}
QcValuesItem *QcGaugeWidget::addValues(float position)
{
QcValuesItem * item = new QcValuesItem(this);
item->setPosition(position);
mItems.append(item);
return item;
}
QcArcItem *QcGaugeWidget::addArc(float position)
{
QcArcItem * item = new QcArcItem(this);
item->setPosition(position);
mItems.append(item);
return item;
}
QcColorBand *QcGaugeWidget::addColorBand(float position)
{
QcColorBand * item = new QcColorBand(this);
item->setPosition(position);
mItems.append(item);
return item;
}
QcNeedleItem *QcGaugeWidget::addNeedle(float position)
{
QcNeedleItem * item = new QcNeedleItem(this);
item->setPosition(position);
mItems.append(item);
return item;
}
QcLabelItem *QcGaugeWidget::addLabel(float position)
{
QcLabelItem * item = new QcLabelItem(this);
item->setPosition(position);
mItems.append(item);
return item;
}
QcGlassItem *QcGaugeWidget::addGlass(float position)
{
QcGlassItem * item = new QcGlassItem(this);
item->setPosition(position);
mItems.append(item);
return item;
}
QcAttitudeMeter *QcGaugeWidget::addAttitudeMeter(float position)
{
QcAttitudeMeter * item = new QcAttitudeMeter(this);
item->setPosition(position);
mItems.append(item);
return item;
}
void QcGaugeWidget::addItem(QcItem *item,float position)
{
// takes parentship of the item
item->setParent(this);
item->setPosition(position);
mItems.append(item);
}
int QcGaugeWidget::removeItem(QcItem *item)
{
return mItems.removeAll(item);
}
QList QcGaugeWidget::items()
{
return mItems;
}
void QcGaugeWidget::paintEvent(QPaintEvent */*paintEvt*/)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
foreach (QcItem * item, mItems) {
item->draw(&painter);
}
}
///////////////////////////////////////////////////////////////////////////////////////////
QcItem::QcItem(QObject *parent) :
QObject(parent)
{
parentWidget = qobject_cast(parent);
mPosition = 50;
}
int QcItem::type()
{
return 50;
}
void QcItem::update()
{
parentWidget->update();
}
float QcItem::position()
{
return mPosition;
}
QRectF QcItem::rect()
{
return mRect;
}
void QcItem::setPosition(float position)
{
if(position>100)
mPosition = 100;
else if(position<0)
mPosition = 0;
else
mPosition = position;
update();
}
QRectF QcItem::adjustRect(float percentage)
{
float r = getRadius(mRect);
float offset = r-(percentage*r)/100.0;
QRectF tmpRect = mRect.adjusted(offset,offset,-offset,-offset);
return tmpRect;
}
float QcItem::getRadius(const QRectF &tmpRect)
{
float r = 0;
if(tmpRect.width()rect();
float r = getRadius(mRect);
mRect.setWidth(2.0*r);
mRect.setHeight(2.0*r);
mRect.moveCenter(parentWidget->rect().center());
return mRect;
}
QPointF QcItem::getPoint(float deg,const QRectF &tmpRect)
{
float r = getRadius(tmpRect);
float xx=cos(qDegreesToRadians(deg))*r;
float yy=sin(qDegreesToRadians(deg))*r;
QPointF pt;
xx=tmpRect.center().x()-xx;
yy=tmpRect.center().y()-yy;
pt.setX(xx);
pt.setY(yy);
return pt;
}
float QcItem::getAngle(const QPointF&pt, const QRectF &tmpRect)
{
float xx=tmpRect.center().x()-pt.x();
float yy=tmpRect.center().y()-pt.y();
return qRadiansToDegrees( atan2(yy,xx));
}
///////////////////////////////////////////////////////////////////////////////////////////
QcScaleItem::QcScaleItem(QObject *parent) :
QcItem(parent)
{
mMinDegree = -45;
mMaxDegree = 225;
mMinValue = 0;
mMaxValue = 100;
}
void QcScaleItem::setValueRange(float minValue, float maxValue)
{
if(!(minValuemMaxValue)
throw (InvalidValueRange);
mMinValue = minValue;
update();
}
void QcScaleItem::setMaxValue(float maxValue)
{
if(maxValuemMaxDegree)
throw (InvalidDegreeRange);
mMinDegree = minDegree;
update();
}
void QcScaleItem::setMaxDegree(float maxDegree)
{
if(maxDegreesetBrush(Qt::NoBrush);
QLinearGradient linearGrad(tmpRect.topLeft(), tmpRect.bottomRight());
for(int i = 0;isetPen(mPen);
painter->setBrush(linearGrad);
painter->drawEllipse(adjustRect(position()));
}
void QcBackgroundItem::addColor(float position, const QColor &color)
{
if(position<0||position>1)
return;
QPair pair;
pair.first = position;
pair.second = color;
mColors.append(pair);
update();
}
void QcBackgroundItem::clearrColors()
{
mColors.clear();
}
///////////////////////////////////////////////////////////////////////////////////////////
QcGlassItem::QcGlassItem(QObject *parent) :
QcItem(parent)
{
setPosition(88);
}
void QcGlassItem::draw(QPainter *painter)
{
resetRect();
QRectF tmpRect1 = adjustRect(position());
QRectF tmpRect2 = tmpRect1;
float r = getRadius(tmpRect1);
tmpRect2.setHeight(r/2.0);
painter->setPen(Qt::NoPen);
QColor clr1 = Qt::gray ;
QColor clr2 = Qt::white;
clr1.setAlphaF(0.2);
clr2.setAlphaF(0.4);
QLinearGradient linearGrad1(tmpRect1.topLeft(), tmpRect1.bottomRight());
linearGrad1.setColorAt(0.1, clr1);
linearGrad1.setColorAt(0.5, clr2);
painter->setBrush(linearGrad1);
painter->drawPie(tmpRect1,0,16*180);
tmpRect2.moveCenter(rect().center());
painter->drawPie(tmpRect2,0,-16*180);
}
///////////////////////////////////////////////////////////////////////////////////////////
QcLabelItem::QcLabelItem(QObject *parent) :
QcItem(parent)
{
setPosition(50);
mAngle = 270;
mText = "%";
mColor = Qt::black;
}
void QcLabelItem::draw(QPainter *painter)
{
resetRect();
QRectF tmpRect = adjustRect(position());
float r = getRadius(rect());
QFont font("Meiryo UI", r/10.0, QFont::Bold);
painter->setFont(font);
painter->setPen(QPen(mColor));
QPointF txtCenter = getPoint(mAngle,tmpRect);
QFontMetrics fMetrics = painter->fo