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博客

相关推荐
共享家95272 小时前
Qt窗口教程(上)
开发语言·qt
俊俊谢2 小时前
OpenCV环境配置(QT 6.6.1 MSVC2019 64bit + OpenCV – 4.12.0)
qt·opencv·msvc
小小鱼儿飞3 小时前
QT Quick QML项目音乐播放器16----无边框窗口拖动、小窗播放、隐藏系统托盘
开发语言·qt
Jay Chou why did3 小时前
14. Qt 自定义控件
qt
-指短琴长-3 小时前
Qt的下载和安装【Windows】
开发语言·windows·qt
硅农深芯6 小时前
如何使用ptqt5实现进度条的动态显示
开发语言·python·qt
Lhan.zzZ7 小时前
Qt数据可视化实战:饼图、线图与表格的完整指南
开发语言·qt·信息可视化
feiyangqingyun8 小时前
Qt/C++编写GB28181服务/前后端分离/定义一套交互协议/视频点播/录像回放和控制/警情通知
c++·qt·交互
虾米Life8 小时前
基于微服务脚手架的视频点播系统 (仿B站) [客户端] -1
c++·qt·微服务·架构
yugi98783810 小时前
基于Qt框架开发多功能视频播放器
开发语言·qt