Qt製作登錄界面-Demo2

Qt製作登錄界面Demo2

Demo1的基礎上增加了登錄成功後動畫反轉進入主界面效果

實現思路:通過QWidget加一個QGraphicsView外殼實現旋轉

效果:
在這裏插入圖片描述

代碼:

TransFormWidget.h

#ifndef TRANSFORMWIDGET_H
#define TRANSFORMWIDGET_H

/**
* @FileName      TransFormWidget.h
* @brief         通過QWidget加一個QGraphicsView外殼實現旋轉,旋轉動畫使用QTimeLine實現
* @author        Kongdemin
* @date          2020-04-24
*/

#include <QGraphicsView>
#include <QWidget>
#include "Demo1/LogIn.h"

class QTimeLine;
class QGraphicsScene;
class QGraphicsProxyWidget;

class TransFormWidget : public QWidget
{
    Q_OBJECT
public:
    explicit TransFormWidget(QWidget *parent = nullptr);

signals:

public slots:
    void performRotateAnimation(int angle);
    void performRotateAnimation2(int angle);

private:
    QGraphicsView *_view;
    QGraphicsScene *_scene;
    QGraphicsProxyWidget *_proxyWidget;
    QTimeLine *_timeLine;
    LogIn *_login;
    int _width;
    int _height;

    QWidget *_widget;
    QTimeLine *_timeLine2;
};

#endif // TRANSFORMWIDGET_H

TransFormWidget.cpp

#include "TransFormWidget.h"
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsProxyWidget>
#include <QTimeLine>
#include <QHBoxLayout>
#include <QLabel>
/**
* @FileName      TransFormWidget.cpp
* @brief         File Description
* @author        Kongdemin
* @date          2020-04-24
*/
TransFormWidget::TransFormWidget(QWidget *parent) : QWidget(parent)
{
    _login = new LogIn;
    _width = _login->width();
    _height = _login->height();
    this->resize(_width, _height);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setStyleSheet("QWidget{border:0px;}");
    
    QHBoxLayout *_layout = new QHBoxLayout(this);
    _layout->setMargin(0);
    _view = new QGraphicsView;
    _layout->addWidget(_view);
    _view->setStyleSheet("QGraphicsView{background:transparent; border:0px;}");
    this->setAttribute(Qt::WA_TranslucentBackground);
    _view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    _view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    _scene = new QGraphicsScene();
    _proxyWidget = _scene->addWidget(_login);
    _view->setScene(_scene);

    _timeLine = new QTimeLine(1000, this);
    _timeLine->setFrameRange(0,90);
    connect(_timeLine, &QTimeLine::frameChanged, this, &TransFormWidget::performRotateAnimation);
    connect(_login, &LogIn::signalSiginSuccessly,[=](){
       _timeLine->start();
    });
    connect(_login, &LogIn::signalClose,[=](){
        this->close();
    });

    _timeLine2 = new QTimeLine(1000, this);
    _timeLine2->setFrameRange(-90,0);
    connect(_timeLine, &QTimeLine::finished,[=](){
        _widget = new QWidget;
        QLabel *label = new QLabel(_widget);
        label->setText(QString::fromLocal8Bit("主界面"));
        label->setStyleSheet("font: 38pt; color:#2c3e50;text-align: center;");
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        label->setGeometry(0,0,_width,_height);
        _widget->setStyleSheet("background-color:#1abc9c;");
        _widget->resize(_width, _height);
        _proxyWidget = _scene->addWidget(_widget);
        _view->setScene(_scene);

        ///先將登錄界面反轉-90度,再反轉正
        QTransform transform;
        transform.translate(_width/2, (this->height()-_height)/2);
        transform.rotate(-90, Qt::YAxis);
        _proxyWidget->setTransform(transform);
        QTransform transform2;
        transform2.translate(-_width/2,0);
        _proxyWidget->setTransform(transform2,true);

        _timeLine2->start();
    });
    connect(_timeLine2, &QTimeLine::frameChanged, this, &TransFormWidget::performRotateAnimation2);
}
///
/// \brief TransFormWidget::performRotateAnimation
/// \param angle
/// \登錄界面按照中心軸旋轉
///
void TransFormWidget::performRotateAnimation(int angle)
{
    qreal angel = angle;
    QTransform transform;
    transform.translate(_width/2, (this->height()-_height)/2);
    transform.rotate(angel, Qt::YAxis);
    _proxyWidget->setTransform(transform);
    QTransform transform2;
    transform2.translate(-_width/2,0);
    _proxyWidget->setTransform(transform2,true);

}
///
/// \brief TransFormWidget::performRotateAnimation2
/// \param angle
/// \主界面按照中心軸方向旋轉
///
void TransFormWidget::performRotateAnimation2(int angle)
{
    qreal angel = angle;
    QTransform transform;
    transform.translate(_width/2, (this->height()-_height)/2);
    transform.rotate(angel, Qt::YAxis);
    _proxyWidget->setTransform(transform);
    QTransform transform2;
    transform2.translate(-_width/2,0);
    _proxyWidget->setTransform(transform2,true);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章