Qt工作笔记-使用qrand与QTime产生随机数
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Qt工作笔记-使用qrand与QTime产生随机数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                程序運行如下:
代碼如下:
widget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget>namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();void timerEvent(QTimerEvent* event);private:Ui::Widget *ui;int m_timer; };widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QTimer> #include <QTime>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);m_timer=startTimer(100);this->setWindowTitle("CSDN IT1995");qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); }void Widget::timerEvent(QTimerEvent *event){if(m_timer=event->timerId()){int num=qrand()%400;ui->lineEdit->setText(tr("m_timerID設置隨機數:")+QString::number(num));} }Widget::~Widget() {delete ui; }main.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }總結
以上是生活随笔為你收集整理的Qt工作笔记-使用qrand与QTime产生随机数的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: C++工作笔记-编译时类型检查与运行时类
- 下一篇: Qt/C++工作笔记-对vector与Q
