QT-Demo-Colck-01
QT += widgets
QT += core HEADERS += \
mainwindow.h SOURCES += \
mainwindow.cpp \
main.cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QWidget> class Clock : public QWidget
{
Q_OBJECT //每个从QT继承的类都添加这个宏定义
public:
Clock(QWidget* parent = );//默认父对象为0,也就是当前窗口为顶级窗口
protected:
void paintEvent(QPaintEvent* e) Q_DECL_OVERRIDE;//只要是覆盖就推荐使用该宏定义
}; #endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QTimer>
#include <QPoint>
#include <QColor>
#include <QTime>
#include <QPainter> //默认参数只在申明时写,定义时不写,将默认参数传递给基类QWidget
Clock::Clock(QWidget *parent):QWidget(parent)
{
QTimer* timer = new QTimer(this);//定时器 //每隔一定时间刷新界面
connect(timer,SIGNAL(timeout()),this,SLOT(update())); timer->start(); setWindowTitle(tr("小小时钟")); resize(,);
} //没有使用到形参,那么定义的时候就不需要指明形参名字
void Clock::paintEvent(QPaintEvent *)
{
//三点围成一个时针
const QPoint hourPoint[]=
{
QPoint(,),
QPoint(-,),
QPoint(,-)
};
//三点围成一个分针
const QPoint minPoint[]=
{
QPoint(,),
QPoint(-,),
QPoint(,-)
};
//三点围成一个秒针
const QPoint secPoint[]=
{
QPoint(,),
QPoint(-,),
QPoint(,-)
};
//分别定义时分秒针的颜色和透明度
QColor hourColor(,,);
QColor minColor(,,,);
QColor secColor(,,); //获取窗体的宽和高的函数首字母是小写
int side = qMin(width(),height()); //获取当前时间
QTime time = QTime::currentTime(); //创建画家
QPainter painter(this); //抗锯齿
painter.setRenderHint(QPainter::Antialiasing); //将坐标移动到客户区中间
painter.translate(width()/,height()/); //设置刻度单位为较短的一边的长度的200分之1
//一定要记得加小数点,否则内面小于200时,就会单位刻度为0
painter.scale(side/200.0,side/200.0); //设置画笔和画刷
painter.setPen(Qt::NoPen);
painter.setBrush(hourColor); //保存画家,和restore()成对使用,保存还没有旋转的画家
painter.save();
//旋转坐标轴
painter.rotate( *(time.hour()+time.minute()/60.0) ); //画多边形
painter.drawConvexPolygon(hourPoint,); //还原画家,和save()成对使用,还原到还没有旋转的画家
painter.restore(); //设置画笔,用来画小时刻度
painter.setPen(hourColor); //旋转12次画小时刻度
for(int i=;i<;i++)
{
painter.drawLine(,,,);
painter.rotate();
} //设置分钟,画刷,画笔
painter.setPen(Qt::NoPen);
painter.setBrush(minColor); //保存旋转之前的画家
painter.save(); //通过旋转坐标轴画分针
painter.rotate(*(time.minute()+time.second()/60.0));
painter.drawConvexPolygon(minPoint,); //还原画家到未旋转的状态
painter.restore(); //设置画笔颜色,用来画分钟刻度
painter.setPen(minColor); //通过旋转60次画分钟刻度
for(int i=;i<;i++)
{
if(i%!=)
painter.drawLine(,,,);
painter.rotate();
} //设置画笔画刷,用来画秒针
painter.setPen(Qt::NoPen);
painter.setBrush(secColor); //保存未旋转的画家
painter.save(); //旋转坐标轴用来画秒针
painter.rotate(*time.second());
painter.drawConvexPolygon(secPoint,); //还原画家到未旋转的状态
painter.restore();
}
#include <QApplication>
#include "mainwindow.h" int main(int argc , char* argv[])
{
QApplication app(argc ,argv); Clock clock ;
clock.show(); return app.exec(); }
QT-Demo-Colck-01的更多相关文章
- 虹软人脸识别——官方 Qt Demo 移植到 Linux
一.前言 最近需要在 Linux 平台下开发一个人脸识别相关的应用,用到了虹软的人脸识别 SDK.之前在 Windows 平台用过,感觉不错,SDK 里面还带了 Demo 可以快速看到效果.打开 Li ...
- Qt Demo Http 解析网址 Openssl
今天练习了一下Qt 解析http协议,在Demo中使用到了Openssl 一上午的时间都是编译openssl,不过还是没有成功,很遗憾,这里整理了有关这个Demo的本件 网盘连接:见下方评论吧,长传太 ...
- 读Qt Demo——Basic Layouts Example
此例程主要展示用代码方式创建控件并用Layout管理类对其进行布局: 例程来自Qt5.2,如过是默认安装,代码位于:C:\Qt\Qt5.2.0\5.2.0\mingw48_32\examples\wi ...
- [QT][DEMO] QTableWidget 设置某一列禁止编辑
例程 : 又是好风景 : http://blog.csdn.net/qiao_yihan/article/details/46413345 关键点: 1.QTableWidgetItem 的 setF ...
- Python 2.7.9 Demo - 019.01.CRUD oracle by cx_Oracle
select #coding=utf-8 #!/usr/bin/python import cx_Oracle; conn = None; cursor = None; try: conn = cx_ ...
- Python 2.7.9 Demo - 003.01.只允许相同缩进
Right #!/usr/bin/python if True: print ("True"); print('Again'); else: print ("False& ...
- Oracle PLSQL Demo - 29.01.Function结构模板 [无入参] [有返回]
CREATE OR REPLACE FUNCTION function_name RETURN DATE AS v_date DATE; BEGIN ; dbms_output.put_line(v_ ...
- Oracle PLSQL Demo - 18.01管道function[查询零散的字段组成list管道返回]
--PACKAGE CREATE OR REPLACE PACKAGE test_141213 is TYPE type_ref IS record( ENAME ), WORK_CITY ), SA ...
- QT+OpenGL(01)--实现三角形渲染
1.openglwidget.ui <ui version="4.0"> <author/> <comment/> <exportmacr ...
- [Part 4] 在Windows 10上源码编译PCL 1.8.1支持VTK和QT,可视化三维点云
本文首发于个人博客https://kezunlin.me/post/2d809f92/,欢迎阅读! Part-4: Compile pcl with vtk qt5 support from sour ...
随机推荐
- HTML5+Css3-webkit-filter
-webkit-filter 现在规范中支持的效果有: grayscale 灰度 sepia 褐色 saturate 饱和度 hue-rotate 色相旋转 invert 反色 opacity 透明度 ...
- Top 100 English Verbs
accept allow ask believe borrow break bring buy can/be able cancel change clean comb complain cough ...
- BroadcastReceiver 案例
BroadcastReceiver 可以接收来自系统和应用的广播,他的生命周期非常简单,只是从对象开始调用他到运行onReceiver方法之后就结束了.要想使用BroadcastReceiver和使用 ...
- hosting company 的 mail , localhost send 不到
不是每一家 hosting 的 mail 都运行你在本地连接发email的. 或许是因为安全的顾虑吧. 总之下次如果发现在本地发不出email,可以试试看 upload to server. 过往经验 ...
- ural 1020 Rope
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...
- Android Canvas.drawText方法中的坐标参数的正确解释
摘要 canvas.drawText(www.jcodecraeer.com, x, y, paint); x和y参数是指定字符串中心的坐标吗?还是左上角的坐标?这个问题的直观印象应该是左上角的坐标, ...
- android merge 标签的使用
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <ToggleButton ...
- logstash multiline
filter { multiline { pattern => "^\s+%{TIMESTAMP_ISO8601}" negate=>true what=>&qu ...
- sigaction 用法实例
sigaction函数的功能是检查或修改与指定信号相关联的处理动作(可同时两种操作). 他是POSIX的信号接口,而signal()是标准C的信号接口(如果程序必须在非POSIX系统上运行,那么就应该 ...
- android TextView 添加下划线
android Textview加下划线 由于新做的一个项目要求有字体带下划线效果,当时看了下其实可以通过图片伪造出那种视觉效果.但是为了体现点技术含量,于是我想用Textview带下划线的效果.方法 ...