Qwt 使用QwtDial绘制钟表

1.概述

QwtDial是Qwt库中的一个类,用于绘制一个可旋转的仪表盘,QwtAnalogClock继承自QwtDial,

模拟时钟。

以下是类继承关系:

2.运行结果

自定义Clock类,继承自QwtAnalogClock,增加一个QTimer,每隔1s更新时间。

cpp 复制代码
    QTimer* timer = new QTimer( this );
    timer->connect( timer, SIGNAL(timeout()), this, SLOT(setCurrentTime()) );
    timer->start( 1000 );
cpp 复制代码
#ifndef CLOCK_H
#define CLOCK_H

#include <QWidget>
#include <QTimer>
#include "qwt_analog_clock.h"
#include "qwt_dial_needle.h"

class Clock : public QwtAnalogClock
{
public:
    Clock( QWidget* parent = NULL );
};

#endif // CLOCK_H


#include "Clock.h"

Clock::Clock( QWidget* parent )
    : QwtAnalogClock( parent )
{
    const QColor knobColor = QColor( Qt::gray ).lighter( 130 );

    for ( int i = 0; i < QwtAnalogClock::NHands; i++ )
    {
        QColor handColor = QColor( Qt::gray ).lighter( 150 );
        int width = 8;

        if ( i == QwtAnalogClock::SecondHand )
        {
            handColor = Qt::gray;
            width = 5;
        }

        QwtDialSimpleNeedle* hand = new QwtDialSimpleNeedle(
            QwtDialSimpleNeedle::Arrow, true, handColor, knobColor );
        hand->setWidth( width );

        setHand( static_cast< QwtAnalogClock::Hand >( i ), hand );
    }

    QTimer* timer = new QTimer( this );
    timer->connect( timer, SIGNAL(timeout()), this, SLOT(setCurrentTime()) );
    timer->start( 1000 );
}

使用:

cpp 复制代码
#include "ClockWidget.h"
#include "ui_ClockWidget.h"
#include "Clock.h"


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;
}


static Clock *g_clock = nullptr;

ClockWidget::ClockWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ClockWidget)
{
    ui->setupUi(this);

    setAutoFillBackground( true );

    setPalette( colorTheme( QColor( Qt::darkGray ).darker( 150 ) ) );

    g_clock = new Clock(this);
    ui->verticalLayout->addWidget(g_clock);
}

ClockWidget::~ClockWidget()
{
    delete ui;
}

3.相关推荐

Qwt 使用QwtDial绘制汽车仪表盘-CSDN博客

相关推荐
feiyangqingyun11 分钟前
全网唯一/Qt结合ffmpeg实现手机端采集摄像头推流到rtsp或rtmp/可切换前置后置摄像头/指定分辨率帧率
qt·智能手机·ffmpeg
随意02328 分钟前
Qt 事件
开发语言·qt
鸥梨菌Honevid36 分钟前
Qt自定义控件(1)——QPaintEvent
开发语言·qt
Mr_Xuhhh19 小时前
网络基础(1)
c语言·开发语言·网络·c++·qt·算法
feiyangqingyun1 天前
Qt音视频开发技巧/推流带旋转角度/rtsprtmp推流/保存文件到MP4/拉流解析旋转角度
qt·音视频·qt旋转角度推流
清醒的兰1 天前
Qt 基于TCP套接字编程
网络·qt·tcp
mahuifa10 天前
PySide环境配置及工具使用
python·qt·环境配置·开发经验·pyside
小灰灰搞电子10 天前
Qt PyQt与PySide技术-C++库的Python绑定
c++·qt·pyqt
Echo``11 天前
12.OpenCV—基础入门
开发语言·c++·人工智能·qt·opencv·计算机视觉
智驾11 天前
QML革命:下一代GUI开发的核心优势详解
qt·qml