1.概述
QwtCompass是Qwt库中的一个类,用于绘制一个指南针样式的仪表盘。它是基于QwtDial类的衍生类,提供了一些额外的功能和样式,用于显示方向和角度。
以下是类继承关系:
2.常用方法
void setScaleArc (double minArc, double maxArc) //设置弧度
void setLineWidth (int) //设置线宽
void setFrameShadow (Shadow) //设置边框样式
void setScaleDraw (QwtRoundScaleDraw *) //设置刻度值以及标签属性
void setNeedle (QwtDialNeedle *) //设置指针
void setRose (QwtCompassRose *rose) //设置rose
void setValue (double value) //设置当前值
3.示例
#ifndef COMPASSWIDGET_H
#define COMPASSWIDGET_H
#include <QWidget>
namespace Ui {
class CompassWidget;
}
class CompassWidget : public QWidget
{
Q_OBJECT
public:
explicit CompassWidget(QWidget *parent = 0);
~CompassWidget();
private:
Ui::CompassWidget *ui;
};
#endif // COMPASSWIDGET_H
#include "CompassWidget.h"
#include "ui_CompassWidget.h"
#include "qwt_compass.h"
#include "qwt_dial_needle.h"
#include "qwt_compass_rose.h"
#include <QTimer>
static QPalette colorTheme( const QColor& base )
{
QPalette palette;
palette.setColor( QPalette::Base, base );
palette.setColor( QPalette::Window, base.darker( 150 ) );
palette.setColor( QPalette::Mid, base.darker( 110 ) );
palette.setColor( QPalette::Light, base.lighter( 170 ) );
palette.setColor( QPalette::Dark, base.darker( 170 ) );
palette.setColor( QPalette::Text, base.darker( 200 ).lighter( 800 ) );
palette.setColor( QPalette::WindowText, base.darker( 200 ) );
return palette;
}
CompassWidget::CompassWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::CompassWidget)
{
ui->setupUi(this);
setPalette( colorTheme( QColor( Qt::darkGray ).darker( 150 ) ) );
QwtCompass *compass = new QwtCompass(this);
compass->setScaleArc( 0.0, 360.0 );
ui->verticalLayout->addWidget(compass);
// 设置圆环的宽度
compass->setLineWidth(4);
// 设置圆环的样式
compass->setFrameShadow(QwtCompass::Raised);
// 设置刻度值以及标签属性
QwtCompassScaleDraw *scaleDraw = new QwtCompassScaleDraw();
scaleDraw->enableComponent(QwtAbstractScaleDraw::Ticks,true); // 显示刻度尺
scaleDraw->enableComponent(QwtAbstractScaleDraw::Labels,true); // 显示文字标签
scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone,false); // 刻度尺圆环是否连续
scaleDraw->setTickLength(QwtScaleDiv::MinorTick,0);
scaleDraw->setTickLength(QwtScaleDiv::MediumTick,0);
scaleDraw->setTickLength(QwtScaleDiv::MajorTick,3);
scaleDraw->setPenWidthF(1);
compass->setScaleDraw(scaleDraw);
// 添加指针
compass->setNeedle(new QwtCompassMagnetNeedle(QwtCompassMagnetNeedle::TriangleStyle, Qt::white, Qt::red));
compass->setValue(180);
//设置rose
// QwtSimpleCompassRose *rose = new QwtSimpleCompassRose(8,1);
// compass->setRose(rose);
QTimer *timer = new QTimer(this);
timer->start(1000);
connect(timer,&QTimer::timeout,[=]{
double value = qrand() % 360;
compass->setValue(value);
});
}
CompassWidget::~CompassWidget()
{
delete ui;
}