QT用qsrand(time(0))产生随机数时出现"time" was not declared in this scope
2014-12-02 16:14
405 查看
于是我们将qsrand(time(0));一句加入构造函数里。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include "time.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
qsrand(time(0)); //每次启动程序产生不同的初始值
startTimer(1000); //其返回值为1,即其timerId为1
startTimer(5000);//其返回值为2,即其timerId为2
startTimer(10000); //其返回值为3,即其timerId为3
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerEvent(QTimerEvent *t) //定时器事件
{
switch(t->timerId()) //判断定时器的句柄
{
case 1 : ui->label->setText(tr("每秒产生一个随机数:%1").arg(qrand()%10));break;
case 2 : ui->label_2->setText(tr("5秒后软件将关闭"));break;
case 3 : qApp->quit();break; //退出系统
}
}
但是我们会发现程序编译时出错了。
根据经验,没有定义,我们第一时间反应是头文件的问题,我们发现刚刚定义的头文件没有出错,那就是才定义的包括time的头文件。查找资料,它的头文件是#include "time.h",于是我们把这个头文件加上去,编译时就不会出错了。此时就不会按同一种顺序产生随机数。
相关文章推荐
- BOOST ThreadPool 编译错误"TIME_UTC was not declared in this scope"
- 昨天的错误修改后,make出现error: 'UINT64_C' was not declared in this scope
- qt-everywhere-opensource-src-4.8.1 出错:error: 'IPV6_MULTICAST_HOPS' was not declared in this scope
- 使用BOOST库 多线程ThreadPool 编译错误"TIME_UTC was not declared in this scope"
- v4l2驱动摄像头 出现错误UINT64_C' was not declared in this scope 解决
- BOOST ThreadPool 编译错误"TIME_UTC was not declared in this scope"
- error: 'UINT64_C' was not declared in this scope
- ndk编译错误:error: 'nullptr' was not declared in this scope
- error: 'memcpy' was not declared in this scope
- c++ error: 'malloc' was not declared in this scope
- 安装fcgi时出现:error: ‘EOF’ was not declared in this scope解决方法
- error: 'EOF' was not declared in this scope的解决办法
- 调用ffmpeg库编译时出现common.h:175:47: error: 'UINT64_C' was not declared in this scope
- 【error: '_1' was not declared in this scope】
- common.h:178:47: error: 'UINT64_C' was not declared in this scope
- error: `cout' was not declared in this scope
- C++ - "'itoa' was not declared in this scope" 错误
- 'setenv' was not declared in this scope
- C++ - 错误: " 'strdup' was not declared in this scope "
- error: 'memcpy' was not declared in this scope的解决