标签:else idg 种子 out 背景 private update Painter cte
#include "mywidget.h" #include "ui_mywidget.h" #include <QPainter> #include <QMouseEvent> #include <QTimer> MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget), flag(true), x(0), y(0) { ui->setupUi(this); pix.load(":/Image/Sunny.jpg"); up.load(":/Image/up.png"); down.load(":/Image/down.png"); this->setWindowFlags(Qt::FramelessWindowHint);// 去掉边框 this->setAttribute(Qt::WA_TranslucentBackground);// 设置背景透明 QTimer *timer = new QTimer(this); timer->start(200); connect(timer,&QTimer::timeout,this,[=](){ // 切换图片 flag = !flag; // 设置时间种子 qsrand(time(NULL)); // 计算飞行路径 x = qrand()%20 + x; y = qrand()%20 + y; if (x > width()){ x = 0; } if (x < 0) { x = width(); } if (y < 0) { y = height(); } if (y > height()) { y = 0; } update(); }); this->showMaximized();//窗口最大化 // x = qrand() % width(); // y = qrand() %height(); } MyWidget::~MyWidget() { delete ui; } void MyWidget::paintEvent(QPaintEvent *) { QPainter p(this); if (flag){ p.drawPixmap(0,0,up);// 在窗口中将图片画出来 } else{ p.drawPixmap(0,0,down); } this->move(x,y); } void MyWidget::mousePressEvent(QMouseEvent *e) { if(e->button() == Qt::LeftButton) { dt_point = e->globalPos() - this->frameGeometry().topLeft();// 求差值 左键按下的点 - 窗口坐上角的点坐标 } else if (e->button() == Qt::RightButton) { this->close(); } } void MyWidget::mouseMoveEvent(QMouseEvent *e) { this->move(e->globalPos() - dt_point);// mvoe x , y 使用的屏幕坐标系,// e->x() , e->y() 窗口的坐标系 Widget }
#ifndef MYWIDGET_H #define MYWIDGET_H #include <QWidget> namespace Ui { class MyWidget; } class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); ~MyWidget(); private: Ui::MyWidget *ui; QPixmap pix; QPixmap up; QPixmap down; QPoint dt_point; bool flag; int x; int y; protected: void paintEvent(QPaintEvent *); void mouseMoveEvent(QMouseEvent *); void mousePressEvent(QMouseEvent *); }; #endif // MYWIDGET_H
标签:else idg 种子 out 背景 private update Painter cte
原文地址:https://www.cnblogs.com/xiangtingshen/p/10761522.html