Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4233608
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-07-25 20:05:49

工程代码:  DigitalClock.rar  


描述:

  通过自动建立 基于 qwidget 类的 ui 工程。在界面中放入LCDNumber ,通过 mousePressEvent 鼠标按下(右键)的处理,退出程序

  1. #include <QtGui/QApplication>
  2. #include "digitalclock.h"

  3. int main(int argc, char *argv[])
  4. {
  5.     QApplication a(argc, argv);
  6.     DigitalClock w;
  7.     w.show();

  8.     return a.exec();
  9. }


  1. #ifndef DIGITALCLOCK_H
  2. #define DIGITALCLOCK_H

  3. #include <QWidget>
  4. #include <QPoint>

  5. namespace Ui {
  6.     class DigitalClock;
  7. }

  8. class DigitalClock : public QWidget
  9. {
  10.     Q_OBJECT

  11. public:
  12.     explicit DigitalClock(QWidget *parent = 0);
  13.     ~DigitalClock();

  14.     void mousePressEvent(QMouseEvent *); //鼠标右键按下,推出程序
  15.     void mouseMoveEvent(QMouseEvent *);
  16. private slots:
  17.     void showTime(); //显示 时钟
  18. private:
  19.     Ui::DigitalClock *ui;
  20.     bool showColon;
  21. };

  22. #endif // DIGITALCLOCK_H

  1. #include "digitalclock.h"
  2. #include "ui_digitalclock.h"
  3. #include <QLCDNumber>
  4. #include <QTimer>
  5. #include <QTime>//系统时间
  6. #include <QMouseEvent>

  7. DigitalClock::DigitalClock(QWidget *parent) :
  8.     QWidget(parent),
  9.     ui(new Ui::DigitalClock)
  10. {
  11.     ui->setupUi(this);

  12.  //add me
  13.    QTimer *timer = new QTimer(this);
  14.    connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
  15.    timer->start(1000);
  16.    showTime();
  17.    showColon = true;
  18. }

  19. DigitalClock::~DigitalClock()
  20. {
  21.     delete ui;
  22. }

  23. void DigitalClock::showTime()
  24. {
  25.     QTime time = QTime::currentTime();//获取当前系统时间,并保存到 time中
  26.     QString text = time.toString("hh:mm"); //装换为字符串格式

  27.     if(showColon) //实现 : 闪烁
  28.                    //当然我们也可以通过 timeevent 来实现 闪烁
  29.     {
  30.         text[2] = ':';
  31.         showColon = false;
  32.    }
  33.     else
  34.    {
  35.        text[2] = '&';
  36.        showColon = true;
  37.     }
  38.     ui->lcdnumber->display(text); //显示
  39. }
  40. void DigitalClock::mousePressEvent(QMouseEvent *e)
  41. {
  42.    // if(e->button() == Qt::LeftButton)//假如左键按下

  43.     if(e->button() == Qt::RightButton)//假如左键按下,退出
  44.     {
  45.         qApp->quit();
  46.     }
  47. }
  48. void DigitalClock::mouseMoveEvent(QMouseEvent *e)
  49. {

  50. }





















阅读(839) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~