当前位置:   article > 正文

Qt4 动画之渐入、渐出_qt4编译qmove

qt4编译qmove

概述

Qt动画模块可实现许多动画效果。其中,QPropertyAnimation Class 是一个控制动画效果的类,诞生自 Qt 4.6 版本。
  本文使用QPropertyAnimation类小小实验一下子窗口的渐入渐出效果。

代码

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

class QLabel;
class QListWidget;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow( QWidget *parent = 0 );
    ~MainWindow();

protected slots:
    void slotBtnEnter(); //点击“Enter”按钮,新增一条子窗口
    void slotBtnLeave(); //点击“Leave”按钮,删除当前选中的子窗口
    void slotLeave();

private:
    void InitWidget();

private:
    QLabel* m_pLbTest1; //初始化用
    QLabel* m_pLbTest2;
    QLabel* m_pLbTest3;
    QLabel* m_pLbTest4;

    QListWidget* m_pListWidget;
};

#endif // MAINWINDOW_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

MainWindow.cpp

#include "mainwindow.h"

#include <QLabel>
#include <QPixmap>
#include <QListWidget>
#include <QListWidgetItem>
#include <QPushButton>
#include <QGridLayout>
#include <QPropertyAnimation>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle( "Test Qt4 Move Animation" );
    setFixedSize( 800, 600 );

    InitWidget();
}

MainWindow::~MainWindow()
{

}

void MainWindow::InitWidget()
{
    //Label
    m_pLbTest1 = new QLabel;
    m_pLbTest2 = new QLabel;
    m_pLbTest3 = new QLabel;
    m_pLbTest4 = new QLabel;
    m_pLbTest1->setScaledContents( true );
    m_pLbTest2->setScaledContents( true );
    m_pLbTest3->setScaledContents( true );
    m_pLbTest4->setScaledContents( true );
    QPixmap pix1( ":/Locomotive.bmp" );
    m_pLbTest1->setPixmap( pix1 );
    m_pLbTest2->setPixmap( pix1 );
    m_pLbTest3->setPixmap( pix1 );
    m_pLbTest4->setPixmap( pix1 );

    //List Widget
    m_pListWidget = new QListWidget;
    m_pListWidget->setStyleSheet("QListWidget::Item{ padding-top:3px; padding-bottom:3px; height:60px;}");

    QListWidgetItem* pItem1 = new QListWidgetItem( m_pListWidget );
    m_pListWidget->addItem( pItem1 );
    m_pListWidget->setItemWidget( pItem1, m_pLbTest1 );

    QListWidgetItem* pItem2 = new QListWidgetItem( m_pListWidget );
    m_pListWidget->addItem( pItem2 );
    m_pListWidget->setItemWidget( pItem2, m_pLbTest2 );

    QListWidgetItem* pItem3 = new QListWidgetItem( m_pListWidget );
    m_pListWidget->addItem( pItem3 );
    m_pListWidget->setItemWidget( pItem3, m_pLbTest3 );

    QListWidgetItem* pItem4= new QListWidgetItem( m_pListWidget );
    m_pListWidget->addItem( pItem4 );
    m_pListWidget->setItemWidget( pItem4, m_pLbTest4 );

    //Buttons
    QPushButton* pBtnEnter = new QPushButton( "Enter" );
    QPushButton* pBtnLeave = new QPushButton( "Leave" );
    pBtnEnter->setFixedHeight( 30 );
    pBtnLeave->setFixedHeight( 30 );
    QHBoxLayout* pBtnLayout = new QHBoxLayout;
    pBtnLayout->addWidget( pBtnEnter );
    pBtnLayout->addWidget( pBtnLeave );

    //Main Layout
    QWidget* pCentralWidget = new QWidget;
    QVBoxLayout* pMainLayout = new QVBoxLayout;
    pMainLayout->addWidget( m_pListWidget );
    pMainLayout->addLayout( pBtnLayout );
    pCentralWidget->setLayout( pMainLayout );
    this->setCentralWidget( pCentralWidget );

    //Connects
    connect( pBtnEnter, SIGNAL( clicked() ), this, SLOT( slotBtnEnter() ) );
    connect( pBtnLeave, SIGNAL( clicked() ), this, SLOT( slotBtnLeave() ) );
}

void MainWindow::slotBtnEnter()
{
    QLabel* pCurWiget = new QLabel;
    pCurWiget->setScaledContents( true );
    QPixmap pix1( ":/Locomotive.bmp" );
    pCurWiget->setPixmap( pix1 );

    QListWidgetItem* pItem = new QListWidgetItem( m_pListWidget );
    m_pListWidget->addItem( pItem );
    m_pListWidget->setItemWidget( pItem, pCurWiget );
    m_pListWidget->setCurrentItem( pItem );

    QPropertyAnimation* pAnimation = new QPropertyAnimation( pCurWiget, "pos" );
    pAnimation->setDuration( 3000 );
    pAnimation->setStartValue( QPointF( -pCurWiget->width(), pCurWiget->pos().y() ) );
    pAnimation->setEndValue( QPointF( 0, pCurWiget->pos().y() ) );
    pAnimation->setEasingCurve( QEasingCurve::Linear );
    pAnimation->start();
}

void MainWindow::slotBtnLeave()
{
    QWidget* pCurWiget = m_pListWidget->itemWidget( m_pListWidget->currentItem() );
    if( !pCurWiget )
        return;

    QPropertyAnimation* pAnimation = new QPropertyAnimation( pCurWiget, "geometry" );
    pAnimation->setDuration( 3000 );
    pAnimation->setEndValue( QRectF( this->width(), pCurWiget->pos().y(), pCurWiget->width(), pCurWiget->height() ) );
    pAnimation->setEasingCurve( QEasingCurve::Linear );
    connect( pAnimation, SIGNAL( finished() ), this, SLOT( slotLeave() ) );
    pAnimation->start();
}

void MainWindow::slotLeave()
{
    m_pListWidget->takeItem( m_pListWidget->currentRow() );
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121

效果展示

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/299606
推荐阅读
相关标签
  

闽ICP备14008679号