欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Qt制作登录界面-Demo2

程序员文章站 2022-05-06 13:18:42
...
Qt制作登录界面Demo2

Demo1的基础上增加了登录成功后动画反转进入主界面效果

实现思路:通过QWidget加一个QGraphicsView外壳实现旋转

效果:
Qt制作登录界面-Demo2

代码:

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);
}