groovy 方式 写的qt时钟代码

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

/**
 * 
 */
package com.suziwen

import com.trolltech.qt.core.QPoint
import com.trolltech.qt.core.QTime
import com.trolltech.qt.core.QTimer
import com.trolltech.qt.gui.QApplication
import com.trolltech.qt.gui.QColor
import com.trolltech.qt.gui.QHideEvent
import com.trolltech.qt.gui.QPaintEvent
import com.trolltech.qt.gui.QPainter
import com.trolltech.qt.gui.QPen
import com.trolltech.qt.gui.QPolygon
import com.trolltech.qt.gui.QShowEvent
import com.trolltech.qt.gui.QWidget

/**
 * @author suziwen
 *
 */



class Clock extends QWidget {
	QTimer qtimer = new QTimer();
	
	static def hourHand = new QPolygon([new QPoint(7,8),new QPoint(-7,new QPoint(0,-30)]);
	static def minuteHand = new QPolygon([new QPoint(7,-70)]);
	static def secondHand = new QPolygon([new QPoint(3,new QPoint(-3,-90)]);
	static def hourColor = new QColor(127,127);
	static def minuteColor = new QColor(0,129,129);
	static def secondColor = new QColor(0,100,100);
	Clock(){
		super();
		qtimer.timeout.connect(this,"update()");
	}
	void hideEvent (QHideEvent event){
		qtimer.stop();
	}
	void showEvent(QShowEvent event){
		qtimer.start(1000);
	}
	void  paintEvent(QPaintEvent e){
		def side = Math.min(this.width(),this.height());
		def time = QTime.currentTime();
		def painter = new QPainter(this);
		painter.translate(this.width()/2,this.height()/2);
		painter.setRenderHint(QPainter.RenderHint.Antialiasing);
		painter.scale(side/200,side/200);
		painter.setPen(QPen.NoPen);
		painter.setBrush(hourColor);
		painter.save();
		painter.rotate(30.0*(time.hour() + time.minute()/60.0));
		painter.drawConvexPolygon(hourHand);
		painter.restore();
		painter.setPen(hourColor);
		for( def i in 0..12){
			painter.drawLine(0,92,96);
			painter.rotate(30.0);
		}
		painter.setPen(QPen.NoPen);
		painter.setBrush(minuteColor);
		painter.save();
		painter.rotate(6.0*(time.minute() + time.second()/60.0));
		painter.drawConvexPolygon(minuteHand);
		painter.restore();
		painter.setPen(minuteColor);
		for(def i in 0..60){
			if(i % 5 != 0 ){
				painter.drawLine(0,94,96);
			}
			painter.rotate(6.0);
		}
		painter.setPen(QPen.NoPen);
		painter.setBrush(secondColor);
		painter.save();
		painter.rotate(6.0*time.second());
		painter.drawConvexPolygon(secondHand);
		painter.restore();
	}

	static main(args) {
		QApplication app = new QApplication(args);
		def clock = new Clock();
		clock.resize(500,500);
		clock.show();
		app.exec();
	}

}

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

相关文章

背景:    8月29日,凌晨4点左右,某服务告警,其中一个...
https://support.smartbear.comeadyapi/docs/soapui/steps/g...
有几个选项可用于执行自定义JMeter脚本并扩展基线JMeter功能...
Scala和Java为静态语言,Groovy为动态语言Scala:函数式编程,...
出处:https://www.jianshu.com/p/ce6f8a1f66f4一、一些内部...
在运行groovy的junit方法时,报了这个错误:java.lang.Excep...