Qt指南针

        Qt写的指南针demo.

      运行结果

        滑动调整指针角度

 实现代码

        h文件

#ifndef COMPASS_H
#define COMPASS_H

#include <QWidget>
#include <QColor>

class Compass : public QWidget
{
    Q_OBJECT
    // 可自定义属性
    Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
    Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor)
    Q_PROPERTY(QColor needleColor READ needleColor WRITE setNeedleColor)
    Q_PROPERTY(QColor directionColor READ directionColor WRITE setDirectionColor)
    Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)
    Q_PROPERTY(double angle READ angle WRITE setAngle)
    Q_PROPERTY(int borderWidth READ borderWidth WRITE setBorderWidth)

public:
    explicit Compass(QWidget *parent = nullptr);

    // 获取和设置属性
    QColor backgroundColor() const;
    void setBackgroundColor(const QColor &color);

    QColor borderColor() const;
    void setBorderColor(const QColor &color);

    QColor needleColor() const;
    void setNeedleColor(const QColor &color);

    QColor directionColor() const;
    void setDirectionColor(const QColor &color);

    QColor textColor() const;
    void setTextColor(const QColor &color);

    double angle() const;
    void setAngle(double angle);

    int borderWidth() const;
    void setBorderWidth(int width);

    QSize sizeHint() const override;
    QSize minimumSizeHint() const override;

protected:
    void paintEvent(QPaintEvent *event) override;

private:
    void drawCompassCircle(QPainter &painter);
    void drawDirectionMarkers(QPainter &painter);
    void drawNeedle(QPainter &painter);
    void drawTechRing(QPainter &painter);

    QColor m_backgroundColor;
    QColor m_borderColor;
    QColor m_needleColor;
    QColor m_directionColor;
    QColor m_textColor;
    double m_angle;
    int m_borderWidth;
};

#endif // COMPASS_H

        c文件

#include "widget.h"
#include <QPainter>
#include <QPainterPath>
#include <QtMath>
#include <QLinearGradient>

Compass::Compass(QWidget *parent) : QWidget(parent),
    m_backgroundColor(QColor(30, 30, 40)),
    m_borderColor(QColor(80, 160, 255)),
    m_needleColor(QColor(255, 80, 80)),
    m_directionColor(QColor(80, 255, 160)),
    m_textColor(QColor(220, 220, 220)),
    m_angle(0),
    m_borderWidth(3)
{
    setMinimumSize(100, 100);
}

QColor Compass::backgroundColor() const { return m_backgroundColor; }
void Compass::setBackgroundColor(const QColor &color) { m_backgroundColor = color; update(); }

QColor Compass::borderColor() const { return m_borderColor; }
void Compass::setBorderColor(const QColor &color) { m_borderColor = color; update(); }

QColor Compass::needleColor() const { return m_needleColor; }
void Compass::setNeedleColor(const QColor &color) { m_needleColor = color; update(); }

QColor Compass::directionColor() const { return m_directionColor; }
void Compass::setDirectionColor(const QColor &color) { m_directionColor = color; update(); }

QColor Compass::textColor() const { return m_textColor; }
void Compass::setTextColor(const QColor &color) { m_textColor = color; update(); }

double Compass::angle() const { return m_angle; }
void Compass::setAngle(double angle) { m_angle = angle; update(); }

int Compass::borderWidth() const { return m_borderWidth; }
void Compass::setBorderWidth(int width) { m_borderWidth = width; update(); }

QSize Compass::sizeHint() const { return QSize(200, 200); }
QSize Compass::minimumSizeHint() const { return QSize(100, 100); }

void Compass::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    // 绘制指南针背景
    drawCompassCircle(painter);

    // 绘制方向标记
    drawDirectionMarkers(painter);

    // 绘制科技感圆环
    drawTechRing(painter);

    // 绘制指针
    drawNeedle(painter);
}

void Compass::drawCompassCircle(QPainter &painter)
{
    int side = qMin(width(), height());
    QRectF outerRect(0, 0, side, side);
    outerRect.moveCenter(rect().center());

    // 绘制背景
    painter.setPen(Qt::NoPen);
    painter.setBrush(m_backgroundColor);
    painter.drawEllipse(outerRect);

    // 绘制边框
    QPen borderPen(m_borderColor);
    borderPen.setWidth(m_borderWidth);
    painter.setPen(borderPen);
    painter.setBrush(Qt::NoBrush);
    painter.drawEllipse(outerRect);

    // 绘制内圆
    QRectF innerRect = outerRect.adjusted(side*0.1, side*0.1, -side*0.1, -side*0.1);
    QRadialGradient gradient(innerRect.center(), innerRect.width()/2);
    gradient.setColorAt(0, QColor(50, 50, 60));
    gradient.setColorAt(1, QColor(20, 20, 30));
    painter.setPen(Qt::NoPen);
    painter.setBrush(gradient);
    painter.drawEllipse(innerRect);
}

void Compass::drawDirectionMarkers(QPainter &painter)
{
    int side = qMin(width(), height());
    QRectF outerRect(0, 0, side, side);
    outerRect.moveCenter(rect().center());

    painter.save();
    painter.translate(outerRect.center());

    QFont font = painter.font();
    font.setPixelSize(side * 0.08);
    font.setBold(true);
    painter.setFont(font);
    painter.setPen(m_textColor);

    QStringList directions = {"N", "E", "S", "W"};
    QStringList subDirections = {"NE", "SE", "SW", "NW"};

    // 绘制主要方向标记
    for (int i = 0; i < 4; ++i) {
        painter.save();
        painter.rotate(i * 90);

        // 绘制刻度线
        QPen pen(m_directionColor);
        pen.setWidth(side * 0.01);
        painter.setPen(pen);
        painter.drawLine(0, -outerRect.height()*0.45, 0, -outerRect.height()*0.35);

        // 绘制方向文字
        painter.translate(0, -outerRect.height()*0.3);
        painter.rotate(-i * 90);
        painter.drawText(QRect(-side*0.1, -side*0.1, side*0.2, side*0.2),
                         Qt::AlignCenter, directions[i]);
        painter.restore();
    }

    // 绘制次要方向标记
    for (int i = 0; i < 4; ++i) {
        painter.save();
        painter.rotate(45 + i * 90);

        // 绘制刻度线
        QPen pen(m_directionColor);
        pen.setWidth(side * 0.005);
        painter.setPen(pen);
        painter.drawLine(0, -outerRect.height()*0.45, 0, -outerRect.height()*0.38);

        // 绘制方向文字
        painter.translate(0, -outerRect.height()*0.33);
        painter.rotate(-45 - i * 90);
        painter.drawText(QRect(-side*0.1, -side*0.1, side*0.2, side*0.2),
                         Qt::AlignCenter, subDirections[i]);
        painter.restore();
    }

    // 绘制更小的刻度
    for (int i = 0; i < 36; ++i) {
        if (i % 9 == 0) continue; // 跳过主要方向

        painter.save();
        painter.rotate(i * 10);

        QPen pen(m_textColor);
        pen.setWidth(side * 0.003);
        painter.setPen(pen);

        if (i % 3 == 0) {
            // 中等刻度
            painter.drawLine(0, -outerRect.height()*0.45, 0, -outerRect.height()*0.4);
        } else {
            // 小刻度
            painter.drawLine(0, -outerRect.height()*0.45, 0, -outerRect.height()*0.42);
        }

        painter.restore();
    }

    painter.restore();
}

void Compass::drawNeedle(QPainter &painter)
{
    int side = qMin(width(), height());
    QRectF outerRect(0, 0, side, side);
    outerRect.moveCenter(rect().center());

    painter.save();
    painter.translate(outerRect.center());
    painter.rotate(m_angle);

    // 创建科技感指针形状
    QPainterPath needlePath;

    // 主指针
    needlePath.moveTo(0, -outerRect.height()*0.35);
    needlePath.lineTo(-outerRect.width()*0.05, -outerRect.height()*0.05);
    needlePath.lineTo(0, 0);
    needlePath.lineTo(outerRect.width()*0.05, -outerRect.height()*0.05);
    needlePath.closeSubpath();

    // 尾翼
    needlePath.moveTo(0, outerRect.height()*0.1);
    needlePath.lineTo(-outerRect.width()*0.03, outerRect.height()*0.2);
    needlePath.lineTo(0, outerRect.height()*0.25);
    needlePath.lineTo(outerRect.width()*0.03, outerRect.height()*0.2);
    needlePath.closeSubpath();

    // 绘制指针
    QLinearGradient gradient(0, -outerRect.height()*0.35, 0, outerRect.height()*0.25);
    gradient.setColorAt(0, m_needleColor.lighter(150));
    gradient.setColorAt(1, m_needleColor.darker(150));

    painter.setPen(QPen(m_needleColor.darker(200), side*0.005));
    painter.setBrush(gradient);
    painter.drawPath(needlePath);

    // 绘制中心圆点
    QRadialGradient centerGradient(0, 0, side*0.05);
    centerGradient.setColorAt(0, Qt::white);
    centerGradient.setColorAt(1, m_needleColor);

    painter.setPen(Qt::NoPen);
    painter.setBrush(centerGradient);
    painter.drawEllipse(QRectF(-side*0.05, -side*0.05, side*0.1, side*0.1));

    painter.restore();
}

void Compass::drawTechRing(QPainter &painter)
{
    int side = qMin(width(), height());
    QRectF outerRect(0, 0, side, side);
    outerRect.moveCenter(rect().center());

    painter.save();
    painter.translate(outerRect.center());

    // 绘制外环科技感装饰
    QPen techPen(m_borderColor);
    techPen.setWidth(side * 0.01);
    painter.setPen(techPen);

    // 绘制分段圆环
    for (int i = 0; i < 8; ++i) {
        painter.save();
        painter.rotate(i * 45);

        QRectF segmentRect(-side*0.5, -side*0.5, side, side);
        segmentRect.adjust(side*0.05, side*0.05, -side*0.05, -side*0.05);

        int startAngle = 10 * 16;
        int spanAngle = 25 * 16;
        painter.drawArc(segmentRect, startAngle, spanAngle);

        painter.restore();
    }

    // 绘制内环科技感装饰
    QRectF innerRingRect(-side*0.3, -side*0.3, side*0.6, side*0.6);
    techPen.setColor(m_directionColor);
    techPen.setWidth(side * 0.005);
    painter.setPen(techPen);
    painter.setBrush(Qt::NoBrush);
    painter.drawEllipse(innerRingRect);

    // 绘制内环点阵
    for (int i = 0; i < 36; ++i) {
        painter.save();
        painter.rotate(i * 10);

        QPointF point(0, -side*0.25);
        painter.setPen(Qt::NoPen);
        painter.setBrush(m_directionColor);
        painter.drawEllipse(point, side*0.005, side*0.005);

        painter.restore();
    }

    painter.restore();
}

        main.c

#include "widget.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QSlider>
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget window;
    QVBoxLayout *layout = new QVBoxLayout(&window);

    // 创建指南针控件
    Compass *compass = new Compass();
    compass->setBackgroundColor(QColor(20, 25, 35));
    compass->setBorderColor(QColor(0, 200, 255));
    compass->setNeedleColor(QColor(255, 60, 60));
    compass->setDirectionColor(QColor(0, 255, 180));
    compass->setTextColor(Qt::white);
    compass->setBorderWidth(4);

    // 创建滑块控制方向
    QSlider *slider = new QSlider(Qt::Horizontal);
    slider->setRange(0, 360);
    slider->setValue(0);

    QObject::connect(slider, &QSlider::valueChanged, [compass](int value) {
        compass->setAngle(value);
    });

    layout->addWidget(compass, 1);
    layout->addWidget(slider);

    window.resize(400, 450);
    window.show();

    return a.exec();
}

### 回答1: Qt是一个跨平台的C++应用程序开发框架。Qt提供了许多UI控件来帮助我们构建应用程序。Qt指南针控件可以被用来实现一个指向某个方向的指南针。 在Qt中,我们可以使用QCompass类来读取设备的方向信息。我们可以把这个方向信息传递给指针控件的角度属性,使它指向正确的方向。指南针控件还可以使用图像来自定义指针的外观,使其更符合应用程序的主题。 为了下载这个控件,我们需要先安装Qt开发环境。然后,我们可以在Qt Creator中打开项目,点击Tool->Forms,直接在Designer中拖拽指南针控件到我们的应用程序窗口中。 如果我们想使用Qt指南针控件的高级功能,比如与传感器交互,我们需要使用Qt Mobility模块。这个模块包含了很多API来帮助我们方便地获取设备的方向和位置信息。 总之,Qt指南针控件是一个有用的UI控件,可以增强我们的应用程序的功能和用户体验。通过简单的设置,我们可以让指南针指向任何方向,并自定义它的外观,使其更符合我们的应用程序的需求。 ### 回答2: Qt 指南针控件是一款基于 Qt 库所开发指南针控件,它能够在用户界面中方便地显示方向指示信息,有助于用户更好地了解当前朝向。下载 Qt 指南针控件主要可以通过以下步骤: 首先,打开 Qt 开发工具,选择 File 菜单下的 New File or Project 进入新建项目界面。在此界面中选择 Qt Widgets Application 作为项目类型,配置好项目的名称和路径等信息后,点击 Next。 接着,在项目配置界面中,勾选要使用的模块和库文件,确保 Qt 指南针控件能够被正常使用。在完成配置后,点击 Next 进入项目概览界面。 在项目概览界面中,确认项目设置无误,点击 Finish 按钮创建新项目。创建后,在项目工程目录中可以找到 Qt 指南针控件的源码文件和相关资源,包括头文件、源码文件和样式表等。 最后,将下载好的 Qt 指南针控件的文件复制到项目目录中,添加到项目中即可正常使用。可以参考 Qt 指南针控件的帮助文档和示例代码,了解更多的控件使用方法和注意事项。总的来说,Qt 指南针控件是一款非常实用的用户界面控件,它可以帮助用户更好地了解方向信息,提高用户体验,值得开发者们去尝试使用。 ### 回答3: Qt框架是一个基于C++编程语言开发工具,可以帮助开发者快速构建跨平台应用程序。其中,Qt指南针控件是其自带的一种功能强大的界面控件,确保用户能够获取实时方向信息。 要下载Qt指南针控件,首先需要安装Qt框架。安装方法如下: 1. 登录Qt官网,进入下载页面 2. 根据所使用的操作系统选择合适版本,比如Windows、MacOS或Linux 3. 下载所需安装文件,一般包括Qt Creator和Qt库文件 4. 根据提示完成安装 在安装完成后,在Qt Creator中即可使用Qt指南针控件。具体使用方法如下: 1. 在Qt Creator中,新建一个Qt Widgets项目 2. 在项目界面中,选择“Design”模式,并选择“添加控件” 3. 在控件库中找到“指南针控件”(或者搜索“QwtCompass”) 4. 将控件拖放到窗口中 5. 可以通过属性编辑器,设置指针的颜色、指向方向等属性 除了通过Qt Creator使用Qt指南针控件,还可以通过安装Qt Qwt库来使用。这是一个基于Qt的轻量级图表和数据可视化工具,其中包括了Qt指南针控件。要使用Qt Qwt库,则需要执行以下步骤: 1. 打开Qt Creator,新建一个Qt Widgets项目 2. 从Qt Qwt官网下载对应版本的库文件 3. 将库文件解压缩,并将其复制到Qt项目的根目录中 4. 在.pro文件中添加如下代码: # Qwt INCLUDEPATH += $$PWD/qwt-6.1.5/include LIBS += -L$$PWD/qwt-6.1.5/lib -lqwt 5. 在Qt Creator中打开项目,添加以下头文件: #include <qwt_compass.h> #include <qwt_compass_rose.h> 通过以上步骤即可使用Qt指南针控件,实现方向指示功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值