不需要别的UI设置,直接放在QT文件中即可

#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QTimer>//定时器头文件
#include <QVector>//容器头文件
#include <QPainter> namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
//explicit显式构造,避免隐式构造
explicit MainWindow(QWidget *parent = nullptr); //构造函数
~MainWindow();//析构函数 private:
Ui::MainWindow *ui;
QTimer *timer;//定时器
QVector<QPoint> point_arry;//保存所有的坐标容器
//界面刷新的时候,会自动调用函数
void paintEvent(QPaintEvent *event);
//定时器处理函数
public slots:
//定义槽函数
void timer_timeout(void); }; #endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//初始化变量
qsrand();
timer = new QTimer(this);
//timer的超时时间与处理函数链接起来,时间来刷新槽函数
connect(timer,SIGNAL(timeout()),this,SLOT(timer_timeout()));
//启动定时器
timer->start();
} MainWindow::~MainWindow()
{
delete ui;
} void MainWindow::paintEvent(QPaintEvent *event)
{
//todo 界面刷新时,执行 将点连成线RER
//准备画笔
QPainter painter(this);
QPen pen;
pen.setWidth();//设置画刷的像素宽度
pen.setBrush(QBrush(Qt::green));//设置画刷的颜色
painter.setPen(pen);
painter.setRenderHint(QPainter::Antialiasing);//画刷抗锯齿
int i;
for(i=;i<point_arry.size();i++){
QPoint p1=point_arry[i-]; //p1 等于point_arry上一点
QPoint p2=point_arry[i]; //p2 等于point_arry目前的点
painter.drawLine(p1,p2);//将p1与p2链接起来
} }
//槽函数的实现
void MainWindow::timer_timeout()
{
//todo 定时器到时执行的任务 移动Y轴
static int x = ;//static 下次不在从零开始
int y;
if(x<this->width()){//如果x现在在主窗口的宽度内
y = this->height()/+rand()%-;//y轴移动的取值,将y值放在height中间
point_arry.push_back(QPoint(x,y));//point_arry的类型就是QPoint类型的
x+=;//每次移动的x轴长度
}
else{//如果x轴的长度超出窗口的长度
int i;
for(i=;i<point_arry.size();i++){
point_arry[i-].setY(point_arry[i].y());//将arry中y的值与上一个y值交换
}
point_arry[point_arry.size()-].setY(this->height()/+rand()%-);//设置目前的y值
}
this->update();
}

最新文章

  1. 【ipv6惹的祸】curl 超时
  2. 将golang程序注册为windows服务
  3. 关于“float”的一次探索--遇到了一个span元素可以设置宽高引发的思考
  4. Grunt设置
  5. .bss 段 block started symbol
  6. Windows 7中使用Eclipse 使用CDT and WinGW 开发C/C++(转载)
  7. 六步实现Spring.NET 与 NHibernate 的整合
  8. C++字符串之一(字符表示)
  9. centos6.5 x86_64安装oracle 11.2.0.3grid
  10. MySql开启远程访问(Linux)
  11. Android数据库--Sqlcipher的使用(一)
  12. extjs Proxy
  13. HTTP各个status code是什么意思【已解决】
  14. 关于ML.NET v0.7的发布说明
  15. MySQL字符串列与整数比较
  16. C#中,使用正则表达式匹配获取所需数据
  17. Mybatis学习4——多对一
  18. inception+archery SQL审核平台
  19. IIS URL Rewrite Module的防盗链规则设置
  20. 总结java中的super和this关键字

热门文章

  1. Hadoop Hive HBase Spark Storm概念解释
  2. Linux:PCBSD系统的安装
  3. Ionic 2 + 手动搭建开发环境教程 【转】
  4. Windows10 VS2017 C++模拟点击按键
  5. Day01_Python学习今日收获
  6. cobbler批量化安装系统
  7. cmd中运行maven -v提示JAVA_HOME的配置问题解决办法
  8. SpringSecurity身份验证基础入门
  9. 腾讯地图api 地址解析 js版
  10. 你好git