天天看點

使用動态圖當背景圖檔,上邊放置各種控件

我們一直使用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 = );
    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 = );

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 = );
    ~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