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

相关推荐
誰能久伴不乏2 小时前
Qt 开发中的父类与父对象的区别和父对象传递:如何选择 `QWidget` 或 `QObject`?
java·开发语言·qt
誰能久伴不乏2 小时前
理解继承与组合的本质:Qt 项目中的设计选择指南
开发语言·qt
抠脚学代码7 小时前
Ubuntu18.6 学习QT问题记录以及虚拟机安装Ubuntu后的设置
qt·学习·ubuntu
小道士写程序17 小时前
Qt 5.12 上读取 .xlsx 文件(Windows 平台)
开发语言·windows·qt
yxc_inspire21 小时前
基于Qt的app开发第十三天
c++·qt·app·tcp·面向对象
潇-xiao1 天前
Qt 按钮类控件(Push Button 与 Radio Button)(1)
c++·qt
追风赶月、1 天前
【QT】认识QT
开发语言·qt
溟洵1 天前
【C++ Qt】窗口(Qt窗口框架、菜单栏QMenuBar)
c++·qt
Wyn_1 天前
【QT】qtdesigner中将控件提升为自定义控件后,css设置样式不生效(已解决,图文详情)
开发语言·qt