zoukankan      html  css  js  c++  java
  • (十二)绘图,不规则窗口,蝴蝶飞舞

    #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
  • 相关阅读:
    C#中Dictionary<TKey,TValue>排序方式
    反射之取类中类的属性、变量名称及其值
    程序测试用的IE浏览器第二次无法加载入口程序的问题及其解决方法
    使用Windows Form 制作一个简易资源管理器
    如何查看自制词典的执行效率
    cocos2dx 3.12 eclipse编辑器切换到Android Studio
    Cordova安装使用
    Activity的启动模式
    踩坑集锦——MVC权限验证
    设计模式学习之路——策略模式
  • 原文地址:https://www.cnblogs.com/xiangtingshen/p/10761522.html
Copyright © 2011-2022 走看看