使用动态图当背景图片,上边放置各种控件

我们一直使用QT制作应用,一般都用图片,或者纯色当做背景,那如何使用动态图当背景呢?
话不多说,直接上代码:

主窗口最终呈现的画面
loginwidget.h

#ifndef LOGINWIDGET_H
#define LOGINWIDGET_H
#include <QWidget>
#include <QLabel>
#include <QResizeEvent>
#include "inforwindow.h"

class LoginWidget : public QWidget
{
    Q_OBJECT
public:
    LoginWidget(QWidget *parent = 0);
    void            resizeEvent(QResizeEvent* size);    //重写resize函数

private:
    InforWindow     *m_pInforWindow = nullptr;          //登录窗口
    QLabel          *m_pBackgroundLabel = nullptr;      //放置背景动态图

};

loginwidget.cpp

#include <QMovie>
#include "loginwidget.h"

LoginWidget::LoginWidget(QWidget *parent)
    : QWidget(parent)
{
    //setWindowState(Qt::WindowFullScreen);//全屏
    //setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);//无边框,置顶
    setMinimumSize(1000,800);

    /*窗口背景*/
    m_pBackgroundLabel = new QLabel(this);
    m_pBackgroundLabel->setScaledContents(true);
    QMovie *movie = new QMovie("../LoginWindow/test.gif");//背景路径,直接放静态图也是可以的
    m_pBackgroundLabel->setMovie(movie);
    movie->start();


    /*登录窗口*/
    m_pInforWindow = new InforWindow(this);
    m_pInforWindow->setAttribute(Qt::WA_TranslucentBackground,true);  //背景透明
    m_pInforWindow->setWindowFlags(Qt::FramelessWindowHint
                                   |Qt::WindowStaysOnTopHint);//无边框,置顶
}

/***************************************************************************
* Function:重写resize函数,保持窗口大小改变时,控件随之改变
* InPut :
* OutPut :
* Return :void
* Other :
* Author : fanxingwang %{CurrentDate:2018.01.24}
***************************************************************************/
void LoginWidget::resizeEvent(QResizeEvent *size)
{
    m_pBackgroundLabel->resize(this->width(),this->height());
    m_pInforWindow->setGeometry((this->width()/4),(this->height()/3+50),
                                (this->width()/2),(this->height()/3));
}

登录窗口代码:
Iinforwindow.h

#ifndef INFORWINDOW_H
#define INFORWINDOW_H

#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QObject>
#include <QPushButton>

class InforWindow : public QWidget
{
    Q_OBJECT
public:
    InforWindow(QWidget *parent = 0);

private:
    QLabel          *m_pUserLabel = nullptr;        //提示输入用户名
    QLabel          *m_pPasswordLabel = nullptr;    //提示输入密码
    QLabel          *m_pInformationLabel = nullptr; //提示按回车

    QLineEdit       *m_pUserLineEdit = nullptr;     //用户名输入框
    QLineEdit       *m_pPasswordLineEdit = nullptr; //密码输入框

    QPushButton     *m_pIsVisibleBtn = nullptr;     //设置密码是否可见按键
    QPushButton     *m_pConfirmBtn = nullptr;       //确认按键

    bool            m_isVisible = false;            //判断按钮状态标志

private slots:
    void            slot_isVisibleBtnClicked();     //是否可见按键响应函数
    void            slot_isConfirmBtnClicked();     //确认按键响应函数
};

#endif // INFORWINDOW_H

Iinforwindow.cpp

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFile>
#include <QDebug>
#include "inforwindow.h"

InforWindow::InforWindow(QWidget *parent)
    : QWidget(parent)
{
    m_pUserLabel = new QLabel(this);
    m_pUserLabel->setText(tr("WHAT'S YOUR NAME?"));

    m_pPasswordLabel = new QLabel(this);
    m_pPasswordLabel->setText(tr("WHAT'S YOUR PASSWORD?"));

    m_pInformationLabel = new QLabel(this);
    m_pInformationLabel->setObjectName(tr("InformationLabel"));
    m_pInformationLabel->setText(tr("OR PRESS ENTER"));

    m_pUserLineEdit = new QLineEdit(this);

    m_pPasswordLineEdit = new QLineEdit(this);
    m_pPasswordLineEdit->setEchoMode(QLineEdit::Password);
    connect(m_pPasswordLineEdit, SIGNAL(returnPressed()),
            this,SLOT(slot_isConfirmBtnClicked()));

    m_pIsVisibleBtn = new QPushButton(this);
    if(!m_isVisible)
    {
    m_pIsVisibleBtn->setObjectName(tr("VisibleBtn"));

    }
    else
    {
        m_pIsVisibleBtn->setObjectName(tr("InVisibleBtn"));
    }

    connect(m_pIsVisibleBtn,SIGNAL(clicked(bool)),
            this,SLOT(slot_isVisibleBtnClicked()));

    m_pConfirmBtn = new QPushButton(this);
    m_pConfirmBtn->setObjectName(tr("ComfirmBtn"));
    connect(m_pConfirmBtn,SIGNAL(clicked(bool)),
            this,SLOT(slot_isConfirmBtnClicked()));

    QVBoxLayout* pVlayout1 = new QVBoxLayout;
    pVlayout1->addWidget(m_pUserLabel);
    pVlayout1->addWidget(m_pUserLineEdit);

    QHBoxLayout* pHlayout1 = new QHBoxLayout;
    pHlayout1->addWidget(m_pPasswordLineEdit);
    pHlayout1->addWidget(m_pIsVisibleBtn);
    pHlayout1->addWidget(m_pConfirmBtn);

    QVBoxLayout* pVlayout2 = new QVBoxLayout;
    pVlayout2->addWidget(m_pPasswordLabel);
    pVlayout2->addLayout(pHlayout1);

    QHBoxLayout* pHlayout2 = new QHBoxLayout;
    pHlayout2->addStretch();
    pHlayout2->addWidget(m_pInformationLabel);

    QVBoxLayout* pVlayout = new QVBoxLayout;
    pVlayout->addLayout(pVlayout1);
    pVlayout->addLayout(pVlayout2);
    pVlayout->addLayout(pHlayout2);

    setLayout(pVlayout);
}

void InforWindow::slot_isVisibleBtnClicked()
{

    if(!m_isVisible)
    {
        m_pPasswordLineEdit->setEchoMode(QLineEdit::Normal);
        m_isVisible = true;
        m_pIsVisibleBtn->setObjectName(tr("InVisibleBtn"));
    }
    else
    {
        m_pPasswordLineEdit->setEchoMode(QLineEdit::Password);
        m_pIsVisibleBtn->setObjectName(tr("VisibleBtn"));
        m_isVisible = false;
    }
    QFile qssfile(":/style.qss");
    qssfile.open(QFile::ReadOnly);
    QString qss;
    qss = qssfile.readAll();
    this->setStyleSheet(qss);
}

void InforWindow::slot_isConfirmBtnClicked()
{
    qDebug()<<"confirm....";
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "loginwidget.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    LoginWidget* m_pLoginWidget = nullptr;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{   
    m_pLoginWidget = new LoginWidget();
    m_pLoginWidget->show();
}

MainWindow::~MainWindow()
{

}

参考:
使用视频当背景1.0:http://blog.csdn.net/fan_xingwang/article/details/79170463
使用视频当背景2.0:http://blog.csdn.net/fan_xingwang/article/details/79170673

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值