Qt事件--数字时钟

 

实验:

1)用Timer显示时间

2)用Timer定时

3)鼠标左键按下控件可随意在桌面上拖动

main.cpp

  1. #include <qapplication.h>
  2. #include "form2.h"
  3. int main( int argc,char ** argv )
  4. {
  5. QApplication a( argc, argv );
  6. Form2 w;
  7. w.show();
  8. a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
  9. return a.exec();
  10. }

form2.cpp

  1. /****************************************************************************
  2. ** Form implementation generated from reading ui file 'form2.ui'
  3. **
  4. ** Created by: The User Interface Compiler ($Id: qt/main.cpp 3.3.6 edited Aug 31 2005 $)
  5. **
  6. ** WARNING! All changes made in this file will be lost!
  7. ****************************************************************************/
  8. #include "form2.h"
  9. #include <qvariant.h>
  10. #include <qlcdnumber.h>
  11. #include <qlayout.h>
  12. #include <qtooltip.h>
  13. #include <qwhatsthis.h>
  14. #include <qimage.h>
  15. #include <qpixmap.h>
  16. #include<qtimer.h>
  17. #include<qdatetime.h>
  18. #include <qpoint.h>
  19. /*
  20. * Constructs a Form2 as a child of 'parent', with the
  21. * name 'name' and widget flags set to 'f'.
  22. *
  23. * The dialog will by default be modeless, unless you set 'modal' to
  24. * TRUE to construct a modal dialog.
  25. */
  26. DigiClock::DigiClock(QWidget * parent, const char * name)
  27. : QLCDNumber(parent,name)
  28. {
  29. QPalette p = palette();
  30. p.setColor(QPalette::Normal, QColorGroup::Background, Qt::blue);
  31. setPalette(p);
  32. QTimer *timer = new QTimer(this);
  33. connect(timer,SIGNAL(timeout()),SLOT(showTime()));
  34. timer->start(1000);
  35. showColon = true;
  36. showTime();
  37. }
  38. void DigiClock::showTime()
  39. {
  40. QTime time = QTime::currentTime();
  41. QString text = time.toString("hh:mm");
  42. if(showColon)
  43. {
  44. text[2] = ':';
  45. showColon = false;
  46. }
  47. else
  48. {
  49. text[2] = ' ';
  50. showColon = true;
  51. }
  52. display(text);
  53. }
  54. Form2::Form2( QWidget* parent, const char* name, bool modal, WFlags fl )
  55. : QDialog( parent, name, modal, fl )
  56. { setMouseTracking (true);
  57. if ( !name )
  58. setName( "Form2" );
  59. lCDNumber2 = new DigiClock( this, "lCDNumber2");
  60. lCDNumber2->setGeometry( QRect( 0, 0, 370, 70 ) );
  61. languageChange();
  62. resize( QSize(369, 70).expandedTo(minimumSizeHint()) );
  63. clearWState( WState_Polished );
  64. LbuttonFlag = false;
  65. }
  66. /*
  67. * Destroys the object and frees any allocated resources
  68. */
  69. Form2::~Form2()
  70. {
  71. // no need to delete child widgets, Qt does it all for us
  72. }
  73. /*
  74. * Sets the strings of the subwidgets using the current
  75. * language.
  76. */
  77. void Form2::languageChange()
  78. {
  79. setCaption( tr( "Form2" ) );
  80. }
  81. void Form2::mousePressEvent(QMouseEvent *e)
  82. {
  83. if(e->button()==QMouseEvent::LeftButton)
  84. {
  85. dragPositon = e->globalPos()-frameGeometry().topLeft();
  86. LbuttonFlag = true;
  87. e->accept();
  88. }
  89. }
  90. void Form2::mouseReleaseEvent(QMouseEvent * e)
  91. {
  92. LbuttonFlag = false;
  93. }
  94. void Form2::mouseMoveEvent(QMouseEvent *e)
  95. {
  96. if(LbuttonFlag)
  97. {
  98. move(e->globalPos()-dragPositon);
  99. qDebug("gx=%d,gy%d,x=%d,y=%d",e->globalX(),e->globalY(),dragPositon.x(),dragPositon.y());
  100. e->accept();
  101. }
  102. }



转载自:http://blog.csdn.net/jianchi88/article/details/7020544

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值