QT定时器

    Qt中定时器的使用有两种方法,

  1. 使用QObject类提供的定时器
  2. 使用QTimer类。

下面分别给出对应的demo

1.

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void showCurrentTime();
protected:
    void timerEvent(QTimerEvent *event);

private:
    Ui::MainWindow *ui;

    int m_curTimeId;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include 
#include 

#pragma execution_character_set("utf-8")

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_curTimeId = startTimer(1000);
    showCurrentTime();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == m_curTimeId)    //判断是否为相应的定时器
    {
        showCurrentTime();
    }
}
void MainWindow::showCurrentTime()
{
    QDate date = QDate::currentDate();
    QTime time = QTime::currentTime();
    int n = date.dayOfWeek();
    QString week;
    switch(n)
    {
    case 1:
        week = tr("星期一");
        break;
    case 2:
        week = tr("星期二");
        break;
    case 3:
        week = tr("星期三");
        break;
    case 4:
        week = tr("星期四");
        break;
    case 5:
        week = tr("星期五");
        break;
    case 6:
        week = tr("星期六");
        break;
    case 7:
        week = tr("星期天");
        break;
    }
    QString dateAndTime = date.toString("yyyy-MM-dd") + QString(" ")
            + time.toString("hh:mm:ss") + QString(" ") + week;
    //时间格式为年-月-日 小时:分钟:秒 星期
    ui->label->setText(dateAndTime);
}

界面设置一个label来显示时间,运行结果如下:

QT定时器_第1张图片

2.

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void showCurrentTime();
private:
    Ui::MainWindow *ui;

    QTimer * m_pTimer;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include 
#include 

#pragma execution_character_set("utf-8")

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    showCurrentTime();

    m_pTimer = new QTimer(this);

    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(showCurrentTime()));
    m_pTimer->start(1000);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showCurrentTime()
{
    QDate date = QDate::currentDate();
    QTime time = QTime::currentTime();
    int n = date.dayOfWeek();
    QString week;
    switch(n)
    {
    case 1:
        week = tr("星期一");
        break;
    case 2:
        week = tr("星期二");
        break;
    case 3:
        week = tr("星期三");
        break;
    case 4:
        week = tr("星期四");
        break;
    case 5:
        week = tr("星期五");
        break;
    case 6:
        week = tr("星期六");
        break;
    case 7:
        week = tr("星期天");
        break;
    }
    QString dateAndTime = date.toString("yyyy-MM-dd") + QString(" ")
            + time.toString("hh:mm:ss") + QString(" ") + week;
    //时间格式为年-月-日 小时:分钟:秒 星期
    ui->label->setText(dateAndTime);
}

 

你可能感兴趣的:(Qt)