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

相关推荐
行十万里人生7 小时前
Qt事件处理:理解处理器、过滤器与事件系统
开发语言·git·qt·华为od·华为·华为云·harmonyos
黑金IT8 小时前
Python3 + Qt5:实现AJAX异步更新UI
qt·ui·ajax
人工智能教学实践9 小时前
基于 yolov8_pyqt5 自适应界面设计的火灾检测系统 demo:毕业设计参考
qt·yolo·课程设计
扎量丙不要犟10 小时前
跨平台的客户端gui到底是选“原生”还是web
前端·javascript·c++·qt·rust·electron·tauri
笑鸿的学习笔记1 天前
qt-Quick3D笔记之官方例程Runtimeloader Example运行笔记
笔记·qt·3d
菜一头包1 天前
线程池以及在QT中的接口使用
c++·qt
R三哥哥啊1 天前
【Qt5】声明之后快速跳转
开发语言·qt·qt5
深蓝海拓1 天前
使用QSqlQueryModel创建交替背景色的表格模型
数据库·qt·pyqt
冰激凌zz2 天前
QT TLS initialization failed
qt
宁静致远20212 天前
Qt u盘自动升级软件
数据库·qt·嵌入式linux开发