1 基本知识git
(1 ) 头文件 :#include <QTimer> //包含定时器 ui
#include <QDateTime> //包含时间处理和显示this
(2) 常见方法code
时间字符串
startimer() //启动定时器get
ToString("yyyy MM dd hh:mm :ss dddd"); 以年月日时分秒格式转成字符串qt
qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); //产生随机种子it
int rand=qrand()%300; //产生 0-300 三百的数event
咱们发现 实际产生随机的方法有点相似ast
咱们看:
srand((unsigned int)time(NULL)); //产生随机种子
产生随机数
res= rand()%n+m; //产生m-n 之间的数
2 案例
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer * timer=new QTimer(this);
id1=startTimer(1000);
id2=startTimer(2000);
id3=startTimer(10000);
QObject::connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(timerUpdate()));
timer->start();
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerUpdate(){
QDateTime ttime=QDateTime::currentDateTime();
QString dat=ttime.toString("yyyy-MM-dd hh:mm:ss dddd");
ui->lineEdit->setText(dat);
int rand=qrand()%300;
ui->lineEdit->move(rand,rand);
}
void MainWindow::timerEvent(QTimerEvent *event)
{
if(event->timerId()==id1)
{
ui->statusBar->showMessage(tr("hi welcome "),20000);
}
else if(event->timerId()==id2)
{
ui->statusBar->showMessage(tr("hi id2"));
}
else{
//qApp->quit();
}
}
Demo15 地址:https://gitee.com/codemaner/qt_learning_record/tree/master