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
  • 相关阅读:
    [BZOJ1625][Usaco2007 Dec]宝石手镯
    [BZOJ1699][Usaco2007 Jan]Balanced Lineup排队
    [BZOJ1606][Usaco2008 Dec]Hay For Sale 购买干草
    [BZOJ1610][Usaco2008 Feb]Line连线游戏
    [BZOJ1609][Usaco2008 Feb]Eating Together麻烦的聚餐
    [BZOJ1602][Usaco2008 Oct]牧场行走
    [BZOJ1601][Usaco2008 Oct]灌水
    [BZOJ1607][Usaco2008 Dec]Patting Heads 轻拍牛头
    [BZOJ1579][Usaco2008 Mar]土地购买
    HDU 4248 A Famous Stone Collector 组合数学dp ****
  • 原文地址:https://www.cnblogs.com/xiangtingshen/p/10761522.html
Copyright © 2011-2022 走看看