1. 程式人生 > >使用動態圖當背景圖片,上邊放置各種控制元件

使用動態圖當背景圖片,上邊放置各種控制元件

我們一直使用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()
{

}